);
impl<'a> MutToken<'a> {
- /// Allow borrowing on the caller's recognisance
+ /// Allows borrowing on the caller's recognisance
///
/// This will let you call
/// [`Ptr::borrow()`]/[`borrow_mut()`](Ptr::borrow_mut)
/// Implemented for `NoAliasSingleton` and the ZST reference tokens,
/// and `&`-references to them.
pub unsafe trait IsRefToken<'a>: Sized + Sealed {
- /// Obtain a new ZST token from something that might be a real object.
+ /// Obtains a new ZST token from something that might be a real object.
#[inline]
fn ref_token(self) -> RefToken<'a> {
RefToken(PhantomData)
/// Implemented for `NoAliasSingleton` and `MutToken`,
/// and `&mut`-references to them.
pub unsafe trait IsMutToken<'a>: IsRefToken<'a> + Sized + Sealed {
- /// Obtain a new ZST token from something that might be a real object.
+ /// Obtains a new ZST token from something that might be a real object.
#[inline]
fn mut_token<'r>(self) -> MutToken<'r> where 'a: 'r {
MutToken(PhantomData)
}
- /// Borrow from a small, fixed, number of multiple `Ptr`
+ /// Borrows from a small, fixed, number of multiple `Ptr`
///
/// Call
/// [`.borrow`](MultiStatic::borrow)
}
}
- /// Borrow from an unknown number of `Ptr` with dynamic runtime checking
+ /// Borrows from an unknown number of `Ptr` with dynamic runtime checking
///
/// Call
/// [`.borrow`](MultiDynamic::borrow)
Box::new(t).into()
}
- /// Borrow the contained data, immutably
+ /// Borrows the contained data, immutably
#[inline]
pub fn borrow<'a>(self, tok: impl IsRefToken<'a>) -> &'a T {
let _ = tok;
}
}
- /// Borrow the contained data, mutably
+ /// Borrows the contained data, mutably
#[inline]
pub fn borrow_mut<'a>(mut self, tok: impl IsMutToken<'a>) -> &'a mut T {
let _ = tok;
}
}
- /// Return the raw data pointer
+ /// Returns the raw data pointer
///
/// Useful if you want to compare pointers, or something.
///
}
impl NoAliasSingleton {
- /// Initialise, by creating the singleton for alias checking
+ /// Initialises, by creating the singleton for alias checking
///
/// # SAFETY
///
}
}
+/// Convenience trait for working with `Option<Ptr<_>>`
pub trait OptionPtrExt<T: ?Sized> {
+ /// Returns the pointer, which will be null iff `self` is `None`
fn as_ptr(self) -> Option<*mut T>;
}
impl<T: ?Sized> OptionPtrExt<T> for Option<Ptr<T>> {
write!(f, "conflicting borrows attempted")
}
}
-impl std::error::Error for BorrowConflict {
-}
+impl std::error::Error for BorrowConflict {}
type BorrowResult<T> = Result<T, BorrowConflict>;
//---------- multi-borrowing ----------
+/// Tracker for multiple borrows (static)
+///
+/// Returned from [`IsMutToken::multi_static`].
pub struct MultiStatic<'a, L> {
tok: MutToken<'a>,
l: L,
Ok(())
}
-pub unsafe trait MultiStaticList {
- fn alias_check_ref(&self, p: NonNull<()>) -> BorrowResult<()>;
- fn alias_check_mut(&self, p: NonNull<()>) -> BorrowResult<()>;
+mod multi_static {
+ use super::*;
+ pub unsafe trait MultiStaticList {
+ fn alias_check_ref(&self, p: NonNull<()>) -> BorrowResult<()>;
+ fn alias_check_mut(&self, p: NonNull<()>) -> BorrowResult<()>;
+ }
}
+use multi_static::MultiStaticList;
+
unsafe impl MultiStaticList for () {
fn alias_check_ref(&self, _: NonNull<()>) -> BorrowResult<()> {
Ok(())
}
impl<'a, L: MultiStaticList> MultiStatic<'a, L> {
+ /// Borrows `p` immutably
+ ///
+ /// Returns the requested borrow, `&T`,
+ /// and `MultiStatic` that can be used for further borrowing.
+ ///
+ /// If `p` has already been mutably borrowed, returns `Err`.
pub fn borrow<'r, T>(self, p: Ptr<T>) -> Result<
(&'r T, MultiStatic<'a, (L, *const ())>),
Self
}
}
+ /// Borrows `p` mutably
+ ///
+ /// Returns the requested borrow, `&mut T`,
+ /// and `MultiStatic` that can be used for further borrowing.
+ ///
+ /// If `p` has already been borrowed, returns `Err`.
pub fn borrow_mut<'r, T>(self, mut p: Ptr<T>) -> Result<
(&'r mut T, MultiStatic<'a, (L, NonNull<()>)>),
Self