pub struct ManagedMutPtr<T: ?Sized> { /* private fields */ }Expand description
A wrapper of NonNullExt that can be used to manage a mutable pointer.
When the check feature is enabled, the crate tracks whether
ManagedMutPtr or ManagedConstPtr 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 mutable reference without unsafe function such as
NonNull::as_mut 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> ManagedMutPtr<T>
impl<T: ?Sized> ManagedMutPtr<T>
Sourcepub unsafe fn new(ptr: NonNullExt<T>) -> Self
pub unsafe fn new(ptr: NonNullExt<T>) -> Self
Creates a ManagedMutPtr from the given pointer.
§Safety
See ManagedMutPtr safety section.
§Examples
use my_ecs::ds::{NonNullExt, ManagedMutPtr};
let mut v = 0;
let ptr = NonNullExt::new(&mut v as *mut i32).unwrap();
let ptr = unsafe { ManagedMutPtr::new(ptr) };Sourcepub const fn dangling() -> Selfwhere
T: Sized,
pub const fn dangling() -> Selfwhere
T: Sized,
Creates a ManagedMutPtr 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::ManagedMutPtr;
let dangling = ManagedMutPtr::<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::ManagedMutPtr;
let dangling = ManagedMutPtr::<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, ManagedMutPtr};
let mut v = 0;
let nne = NonNullExt::new(&mut v as *mut i32).unwrap();
let ptr = unsafe { ManagedMutPtr::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) -> *mut T
pub fn as_ptr(&self) -> *mut T
Creates a raw poitner from this pointer.
§Examples
use my_ecs::ds::{NonNullExt, ManagedMutPtr};
let mut v = 0;
let nne = NonNullExt::new(&mut v as *mut i32).unwrap();
let ptr = unsafe { ManagedMutPtr::new(nne) };
assert_eq!(ptr.as_ptr(), nne.as_ptr());Sourcepub fn into_mut<'a>(self) -> &'a mut T
pub fn into_mut<'a>(self) -> &'a mut T
Converts the pointer into a mutable reference.
Note that trace of the address by check feature ends by consuming the
pointer.
§Examples
use my_ecs::ds::{NonNullExt, ManagedMutPtr};
let mut v = 0;
let nne = NonNullExt::new(&mut v as *mut i32).unwrap();
let ptr = unsafe { ManagedMutPtr::new(nne) };
assert_eq!(ptr.into_mut(), &0);Sourcepub fn cast<U>(self) -> ManagedMutPtr<U>
pub fn cast<U>(self) -> ManagedMutPtr<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, ManagedMutPtr};
let mut v = 0x1234_5678;
let nne = NonNullExt::new(&mut v as *mut i32).unwrap();
let ptr = unsafe { ManagedMutPtr::new(nne) };
let ptr = ptr.cast::<[u8; 4]>();
let ref_v = ptr.into_mut();
assert_eq!(*ref_v, i32::to_ne_bytes(v));Sourcepub fn cast_const(self) -> ManagedConstPtr<T>
pub fn cast_const(self) -> ManagedConstPtr<T>
Changes constness without changing the type.
§Examples
use my_ecs::ds::{NonNullExt, ManagedMutPtr};
let mut v = 0;
let nne = NonNullExt::new(&mut v as *mut i32).unwrap();
let ptr = unsafe { ManagedMutPtr::new(nne) };
let ptr = ptr.cast_const();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, ManagedMutPtr};
let arr: [char; 2] = ['a', 'b'];
let nne = NonNullExt::new(arr.as_ptr().cast_mut()).unwrap();
let ptr = unsafe { ManagedMutPtr::new(nne) };
unsafe { assert_eq!(*ptr.add(1), 'b') };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, ManagedMutPtr};
let arr: [char; 2] = ['a', 'b'];
let nne = NonNullExt::new((&arr[1] as *const char).cast_mut()).unwrap();
let ptr = unsafe { ManagedMutPtr::new(nne) };
unsafe { assert_eq!(*ptr.sub(1), 'a') };Trait Implementations§
Source§impl<T: ?Sized> Debug for ManagedMutPtr<T>
impl<T: ?Sized> Debug for ManagedMutPtr<T>
Source§impl<T: ?Sized> Deref for ManagedMutPtr<T>
impl<T: ?Sized> Deref for ManagedMutPtr<T>
Source§impl<T: ?Sized> DerefMut for ManagedMutPtr<T>
impl<T: ?Sized> DerefMut for ManagedMutPtr<T>
Source§impl<T: ?Sized> Hash for ManagedMutPtr<T>
impl<T: ?Sized> Hash for ManagedMutPtr<T>
Source§impl<T: ?Sized> Ord for ManagedMutPtr<T>
impl<T: ?Sized> Ord for ManagedMutPtr<T>
Source§impl<T: ?Sized> PartialEq for ManagedMutPtr<T>
impl<T: ?Sized> PartialEq for ManagedMutPtr<T>
Source§impl<T: ?Sized> PartialOrd for ManagedMutPtr<T>
impl<T: ?Sized> PartialOrd for ManagedMutPtr<T>
impl<T: ?Sized> Eq for ManagedMutPtr<T>
impl<T: ?Sized + Send> Send for ManagedMutPtr<T>
Auto Trait Implementations§
impl<T> Freeze for ManagedMutPtr<T>where
T: ?Sized,
impl<T> RefUnwindSafe for ManagedMutPtr<T>where
T: RefUnwindSafe + ?Sized,
impl<T> !Sync for ManagedMutPtr<T>
impl<T> Unpin for ManagedMutPtr<T>where
T: ?Sized,
impl<T> UnwindSafe for ManagedMutPtr<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> 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