Skip to content

Commit

Permalink
add ref wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
zephyrchien committed Apr 10, 2022
1 parent 25ff9f4 commit 8f898a1
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
2 changes: 1 addition & 1 deletion kaminari/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "kaminari"
version = "0.4.0"
version = "0.4.1"
edition = "2021"
authors = ["zephyr <[email protected]>"]
keywords = ["lightws", "network"]
Expand Down
1 change: 1 addition & 0 deletions kaminari/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ pub mod nop;
pub mod tls;
pub mod mix;
pub mod opt;
pub mod trick;
35 changes: 35 additions & 0 deletions kaminari/src/trick.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use core::ops::Deref;

// Safety:
// pointer is not null once inited(comes from an immutable ref)
// pointee memory is always valid during the eventloop
pub struct Ref<T>(*const T);

unsafe impl<T: Send + Sync> Send for Ref<T> {}
unsafe impl<T: Send + Sync> Sync for Ref<T> {}

impl<T> Copy for Ref<T> {}

impl<T> Clone for Ref<T> {
fn clone(&self) -> Self { *self }
}

impl<T> Deref for Ref<T> {
type Target = T;

#[inline]
fn deref(&self) -> &Self::Target { unsafe { &*self.0 } }
}

impl<T> AsRef<T> for Ref<T> {
#[inline]
fn as_ref(&self) -> &T { unsafe { &*self.0 } }
}

impl<T> From<&T> for Ref<T> {
fn from(x: &T) -> Self { Ref(x as *const _) }
}

impl<T> Ref<T> {
pub const fn new(x: &T) -> Self { Self(x as *const _) }
}

0 comments on commit 8f898a1

Please sign in to comment.