pub struct SendSyncPtr<T: ?Sized>(/* private fields */);Expand description
A pointer that implements Send and Sync regardless of whether T
implements both Send and Sync.
§Safety
This pointer can be sent to another worker. Owner must guarantee that
sending the pointer is safe. For instance, if you are controlling access to
pointer over workers completely, it will be safe in terms of Send and
Sync.
Implementations§
Source§impl<T: ?Sized> SendSyncPtr<T>
impl<T: ?Sized> SendSyncPtr<T>
Sourcepub const fn new(ptr: NonNull<T>) -> Self
pub const fn new(ptr: NonNull<T>) -> Self
Creates a SendSyncPtr by wrapping the given pointer.
§Examples
use my_ecs::ds::SendSyncPtr;
use std::ptr::NonNull;
let mut v = 0;
let ptr = NonNull::new(&mut v as *mut i32).unwrap();
let ptr = SendSyncPtr::new(ptr);Sourcepub const fn dangling() -> Selfwhere
T: Sized,
pub const fn dangling() -> Selfwhere
T: Sized,
Creates a SendSyncPtr that is dangling, but well-aligned.
In many Rust functions, they require aligned pointers even if they are some trash values. This function will be usuful in that cases.
§Examples
use my_ecs::ds::SendSyncPtr;
use std::ptr::NonNull;
let dangling = SendSyncPtr::<i32>::dangling();Sourcepub fn is_dangling(&self) -> boolwhere
T: Sized,
pub fn is_dangling(&self) -> boolwhere
T: Sized,
Returns true if the pointer is dangling.
§Examples
use my_ecs::ds::SendSyncPtr;
let dangling = SendSyncPtr::<i32>::dangling();
assert!(dangling.is_dangling());Sourcepub const fn as_nonnull(self) -> NonNull<T>
pub const fn as_nonnull(self) -> NonNull<T>
Sourcepub const fn as_ptr(self) -> *mut T
pub const fn as_ptr(self) -> *mut T
Creates a raw pointer from this pointer.
§Examples
use my_ecs::ds::SendSyncPtr;
use std::ptr::NonNull;
let mut v = 0;
let nn = NonNull::new(&mut v as *mut i32).unwrap();
let ptr = SendSyncPtr::new(nn);
assert_eq!(ptr.as_ptr(), nn.as_ptr());Sourcepub const unsafe fn as_ref<'a>(&self) -> &'a T
pub const unsafe fn as_ref<'a>(&self) -> &'a T
Returns a shared reference to the value.
§Safety
See NonNull::as_ref.
§Examples
use my_ecs::ds::SendSyncPtr;
use std::ptr::NonNull;
let mut v = 0;
let ptr = NonNull::new(&mut v as *mut i32).unwrap();
let ptr = SendSyncPtr::new(ptr);
let ref_v = unsafe { ptr.as_ref() };
assert_eq!(ref_v, &v);Sourcepub unsafe fn as_mut<'a>(&mut self) -> &'a mut T
pub unsafe fn as_mut<'a>(&mut self) -> &'a mut T
Returns a mutable reference to the value.
§Safety
See NonNull::as_mut.
§Examples
use my_ecs::ds::SendSyncPtr;
use std::ptr::NonNull;
let mut v = 0;
let ptr = NonNull::new(&mut v as *mut i32).unwrap();
let mut ptr = SendSyncPtr::new(ptr);
let mut_v = unsafe { ptr.as_mut() };
assert_eq!(mut_v, &mut v);Sourcepub const unsafe fn add(self, count: usize) -> Selfwhere
T: Sized,
pub const unsafe fn add(self, count: usize) -> Selfwhere
T: Sized,
Adds an offset to the pointer then returns the result.
Note that count is in units of T. For example, count = 3 means
12 bytes offset if T is i32.
§Safety
See NonNull::add.
§Examples
use my_ecs::ds::SendSyncPtr;
use std::ptr::NonNull;
let arr: [char; 3] = ['a', 'b', 'c'];
let ptr = NonNull::new(arr.as_ptr().cast_mut()).unwrap();
let ptr = SendSyncPtr::new(ptr);
let ref_v = unsafe { ptr.add(1).as_ref() };
assert_eq!(ref_v, &'b');
let ref_v = unsafe { ptr.add(2).as_ref() };
assert_eq!(ref_v, &'c');Sourcepub const unsafe fn sub(self, count: usize) -> Selfwhere
T: Sized,
pub const unsafe fn sub(self, count: usize) -> Selfwhere
T: Sized,
Subtracts an offset from the pointer then returns the result.
Note that count is in units of T. For example, count = 3 means
12 bytes offset if T is i32.
§Safety
See NonNull::sub.
§Examples
use my_ecs::ds::SendSyncPtr;
use std::ptr::NonNull;
let arr: [char; 3] = ['a', 'b', 'c'];
let ptr = NonNull::new((&arr[2] as *const char).cast_mut()).unwrap();
let ptr = SendSyncPtr::new(ptr);
let ref_v = unsafe { ptr.sub(1).as_ref() };
assert_eq!(ref_v, &'b');
let ref_v = unsafe { ptr.sub(2).as_ref() };
assert_eq!(ref_v, &'a');Sourcepub const fn cast<U>(self) -> SendSyncPtr<U>
pub const fn cast<U>(self) -> SendSyncPtr<U>
Casts the pointer to another type.
§Examples
use my_ecs::ds::SendSyncPtr;
use std::ptr::NonNull;
let mut v = 0x1234_5678;
let ptr = NonNull::new(&mut v as *mut i32).unwrap();
let ptr = SendSyncPtr::new(ptr);
let ptr = ptr.cast::<[u8; 4]>();
let ref_v = unsafe { ptr.as_ref() };
assert_eq!(*ref_v, i32::to_ne_bytes(v));Trait Implementations§
Source§impl<T: ?Sized> Clone for SendSyncPtr<T>
impl<T: ?Sized> Clone for SendSyncPtr<T>
Source§impl<T: ?Sized> Hash for SendSyncPtr<T>
impl<T: ?Sized> Hash for SendSyncPtr<T>
Source§impl<T: ?Sized> Ord for SendSyncPtr<T>
impl<T: ?Sized> Ord for SendSyncPtr<T>
Source§impl<T: ?Sized> PartialEq for SendSyncPtr<T>
impl<T: ?Sized> PartialEq for SendSyncPtr<T>
Source§impl<T: ?Sized> PartialOrd for SendSyncPtr<T>
impl<T: ?Sized> PartialOrd for SendSyncPtr<T>
impl<T: ?Sized> Copy for SendSyncPtr<T>
impl<T: ?Sized> Eq for SendSyncPtr<T>
impl<T: ?Sized> Send for SendSyncPtr<T>
impl<T: ?Sized> Sync for SendSyncPtr<T>
Auto Trait Implementations§
impl<T> Freeze for SendSyncPtr<T>where
T: ?Sized,
impl<T> RefUnwindSafe for SendSyncPtr<T>where
T: RefUnwindSafe + ?Sized,
impl<T> Unpin for SendSyncPtr<T>where
T: ?Sized,
impl<T> UnwindSafe for SendSyncPtr<T>where
T: RefUnwindSafe + ?Sized,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more