pub struct ManagedConstPtr<T: ?Sized> { /* private fields */ }Expand description
A wrapper of NonNullExt that can be used to manage a constant pointer.
When the check feature is enabled, the crate tracks whether
ManagedMutPtr that has the same address is being created while the
pointer is alive. This could be useful when you need extra debugging
facility than NonNullExt.
§Safety
The pointer is used as a shared reference without unsafe function such as
NonNull::as_ref because the pointer is completely managed. Therefore,
You must make sure that the pointer will not violate any conditions of
Pointer to reference conversion in std::ptr document.
Implementations§
Source§impl<T: ?Sized> ManagedConstPtr<T>
impl<T: ?Sized> ManagedConstPtr<T>
Sourcepub unsafe fn new(ptr: NonNullExt<T>) -> Self
pub unsafe fn new(ptr: NonNullExt<T>) -> Self
Creates a ManagedConstPtr from the given pointer.
§Safety
See ManagedConstPtr safety section.
§Examples
use my_ecs::ds::{NonNullExt, ManagedConstPtr};
let mut v = 0;
let ptr = NonNullExt::new(&mut v as *mut i32).unwrap();
let ptr = unsafe { ManagedConstPtr::new(ptr) };Sourcepub const fn dangling() -> Selfwhere
T: Sized,
pub const fn dangling() -> Selfwhere
T: Sized,
Creates a ManagedConstPtr 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::ManagedConstPtr;
let dangling = ManagedConstPtr::<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::ManagedConstPtr;
let dangling = ManagedConstPtr::<i32>::dangling();
assert!(dangling.is_dangling());Sourcepub fn as_nonnullext(&self) -> NonNullExt<T>
pub fn as_nonnullext(&self) -> NonNullExt<T>
Creates a NonNullExt from this pointer.
§Examples
use my_ecs::ds::{NonNullExt, ManagedConstPtr};
let mut v = 0;
let nne = NonNullExt::new(&mut v as *mut i32).unwrap();
let ptr = unsafe { ManagedConstPtr::new(nne) };
assert_eq!(ptr.as_nonnullext(), nne);Sourcepub fn as_nonnull(&self) -> NonNull<T>
pub fn as_nonnull(&self) -> NonNull<T>
Sourcepub fn as_ptr(&self) -> *const T
pub fn as_ptr(&self) -> *const T
Creates a raw poitner from this pointer.
§Examples
use my_ecs::ds::{NonNullExt, ManagedConstPtr};
let mut v = 0;
let nne = NonNullExt::new(&mut v as *mut i32).unwrap();
let ptr = unsafe { ManagedConstPtr::new(nne) };
assert_eq!(ptr.as_ptr(), nne.as_ptr());Sourcepub fn into_ref<'a>(self) -> &'a T
pub fn into_ref<'a>(self) -> &'a T
Converts the pointer into a shared reference.
Note that trace of the address by check feature ends by consuming the
pointer.
§Examples
use my_ecs::ds::{NonNullExt, ManagedConstPtr};
let mut v = 0;
let nne = NonNullExt::new(&mut v as *mut i32).unwrap();
let ptr = unsafe { ManagedConstPtr::new(nne) };
assert_eq!(ptr.into_ref(), &0);Sourcepub fn cast<U>(self) -> ManagedConstPtr<U>
pub fn cast<U>(self) -> ManagedConstPtr<U>
Casts the pointer to another type.
This method doesn’t break the trace of the address by check feature.
But internal type information is reset. See NonNullExt::cast.
§Examples
use my_ecs::ds::{NonNullExt, ManagedConstPtr};
let mut v = 0x1234_5678;
let nne = NonNullExt::new(&mut v as *mut i32).unwrap();
let ptr = unsafe { ManagedConstPtr::new(nne) };
let ptr = ptr.cast::<[u8; 4]>();
let ref_v = ptr.into_ref();
assert_eq!(*ref_v, i32::to_ne_bytes(v));Sourcepub unsafe fn add(self, count: usize) -> Selfwhere
T: Sized,
pub 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::{NonNullExt, ManagedConstPtr};
let arr: [char; 3] = ['a', 'b', 'c'];
let nne = NonNullExt::new(arr.as_ptr().cast_mut()).unwrap();
let ptr = unsafe { ManagedConstPtr::new(nne) };
unsafe {
assert_eq!(*ptr.add(1), 'b');
assert_eq!(*ptr.add(2), 'c');
}Sourcepub unsafe fn sub(self, count: usize) -> Selfwhere
T: Sized,
pub 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::{NonNullExt, ManagedConstPtr};
let arr: [char; 3] = ['a', 'b', 'c'];
let nne = NonNullExt::new((&arr[2] as *const char).cast_mut()).unwrap();
let ptr = unsafe { ManagedConstPtr::new(nne) };
unsafe {
assert_eq!(*ptr.sub(1), 'b');
assert_eq!(*ptr.sub(2), 'a');
}Trait Implementations§
Source§impl<T: ?Sized> Clone for ManagedConstPtr<T>
impl<T: ?Sized> Clone for ManagedConstPtr<T>
Source§impl<T: ?Sized> Debug for ManagedConstPtr<T>
impl<T: ?Sized> Debug for ManagedConstPtr<T>
Source§impl<T: ?Sized> Deref for ManagedConstPtr<T>
impl<T: ?Sized> Deref for ManagedConstPtr<T>
Source§impl<T: ?Sized> Hash for ManagedConstPtr<T>
impl<T: ?Sized> Hash for ManagedConstPtr<T>
Source§impl<T: ?Sized> Ord for ManagedConstPtr<T>
impl<T: ?Sized> Ord for ManagedConstPtr<T>
Source§impl<T: ?Sized> PartialEq for ManagedConstPtr<T>
impl<T: ?Sized> PartialEq for ManagedConstPtr<T>
Source§impl<T: ?Sized> PartialOrd for ManagedConstPtr<T>
impl<T: ?Sized> PartialOrd for ManagedConstPtr<T>
impl<T: ?Sized> Copy for ManagedConstPtr<T>
impl<T: ?Sized> Eq for ManagedConstPtr<T>
impl<T: ?Sized + Send> Send for ManagedConstPtr<T>
Auto Trait Implementations§
impl<T> Freeze for ManagedConstPtr<T>where
T: ?Sized,
impl<T> RefUnwindSafe for ManagedConstPtr<T>where
T: RefUnwindSafe + ?Sized,
impl<T> !Sync for ManagedConstPtr<T>
impl<T> Unpin for ManagedConstPtr<T>where
T: ?Sized,
impl<T> UnwindSafe for ManagedConstPtr<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