forked from zhboner/realm
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8c32ac3
commit bb851db
Showing
4 changed files
with
43 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,40 @@ | ||
use core::ops::Deref; | ||
|
||
use cfg_if::cfg_if; | ||
|
||
use super::{Endpoint, RemoteAddr, ConnectOpts}; | ||
|
||
// Safety: | ||
// pointer is not null once inited(comes from an immutable ref) | ||
// pointee memory is always valid during the eventloop | ||
#[repr(transparent)] | ||
pub struct Ref<T>(*const T); | ||
|
||
unsafe impl<T> Send for Ref<T> {} | ||
unsafe impl<T> Sync for Ref<T> {} | ||
|
||
macro_rules! ptr_wrap { | ||
($old: ident,$new: ident) => { | ||
#[derive(Clone, Copy)] | ||
pub struct $new { | ||
ptr: *const $old, | ||
} | ||
|
||
unsafe impl Send for $new {} | ||
unsafe impl Sync for $new {} | ||
|
||
impl AsRef<$old> for $new { | ||
#[inline] | ||
fn as_ref(&self) -> &$old { | ||
unsafe { &*self.ptr } | ||
} | ||
} | ||
|
||
impl Deref for $new { | ||
type Target = $old; | ||
|
||
#[inline] | ||
fn deref(&self) -> &Self::Target { | ||
self.as_ref() | ||
} | ||
} | ||
|
||
impl From<&$old> for $new { | ||
fn from(ptr: &$old) -> Self { | ||
$new { ptr } | ||
} | ||
} | ||
}; | ||
impl<T> Copy for Ref<T> {} | ||
|
||
impl<T> Clone for Ref<T> { | ||
fn clone(&self) -> Self { | ||
*self | ||
} | ||
} | ||
|
||
ptr_wrap!(Endpoint, EndpointRef); | ||
ptr_wrap!(RemoteAddr, RemoteAddrRef); | ||
ptr_wrap!(ConnectOpts, ConnectOptsRef); | ||
impl<T> Deref for Ref<T> { | ||
type Target = T; | ||
|
||
cfg_if! { | ||
if #[cfg(feature = "udp")] { | ||
use std::sync::{Arc,RwLock}; | ||
use std::collections::HashMap; | ||
use std::net::SocketAddr; | ||
use tokio::net::UdpSocket; | ||
#[inline] | ||
fn deref(&self) -> &Self::Target { | ||
unsafe { &*self.0 } | ||
} | ||
} | ||
|
||
// client <--> allocated socket | ||
pub type SockMap = RwLock<HashMap<SocketAddr, Arc<UdpSocket>>>; | ||
impl<T> AsRef<T> for Ref<T> { | ||
#[inline] | ||
fn as_ref(&self) -> &T { | ||
unsafe { &*self.0 } | ||
} | ||
} | ||
|
||
ptr_wrap!(UdpSocket, UdpSocketRef); | ||
ptr_wrap!(SockMap, SockMapRef); | ||
impl<T> From<&T> for Ref<T> { | ||
fn from(x: &T) -> Self { | ||
Ref(x as *const _) | ||
} | ||
} |