/// **Pointer to manually-memory-managed `T`**
///
/// `Ptr<T>` usually refers to an item on the heap, like `Box<T>`.
-/// It can be made from `T` with [`Ptr::new_heap()`].
+/// It can be made from `T` with [`Ptr::new_heap()`],
+/// or [`From<Box>`](#impl-From%3CBox%3CT%3E%3E-for-Ptr%3CT%3E).
///
/// To access the contained data,
/// [`Ptr::borrow()`]
impl<T: ?Sized> Copy for Ptr<T> {}
impl<T: ?Sized> Ptr<T> {
+ /// Makes a new `Ptr`, moving the `T` to a new heap allocation
pub fn new_heap(t: T) -> Self
where T: Sized
{
Box::new(t).into()
}
+ /// Borrow the contained data, immutably
#[inline]
pub fn borrow<'a>(self, tok: impl IsRefToken<'a>) -> &'a T {
let _ = tok;
}
}
+ /// Borrow the contained data, mutably
#[inline]
pub fn borrow_mut<'a>(mut self, tok: impl IsMutToken<'a>) -> &'a mut T {
let _ = tok;
}
}
- /// Useful if you want to compare pointers, or something
+ /// Return the raw data pointer
+ ///
+ /// Useful if you want to compare pointers, or something.
+ ///
+ /// If `Ptr` was made with `new_heap` or `From<Box<_>>`,
+ /// this pointer has the same properties as `Box`'s.
+ /// But of course many copies may exist.
pub fn as_ptr(self) -> NonNull<T> {
self.ptr
}
#[test]
fn unsize() {
let mut noalias = unsafe { NoAliasSingleton::init() };
- let p = Ptr::new_heap("hi");
+ let b: Box<str> = "hi".into();
+ let p: Ptr<str> = Ptr::from(b);
println!("{}", p.borrow(&noalias));
unsafe { p.free_heap(&mut noalias) };
}