pub struct RwAnchor<'a, T: ?Sized> { /* private fields */ }
Expand description
An !Send
mutable anchor with overlapping immutable borrows.
Use this to capture mutable references in a single-threaded environment.
§Deadlocks
Iff there is a currently active borrow, then dropping this anchor will cause a deadlock as last resort measure to prevent UB:
use ref_portals::rc::RwAnchor;
let mut x = "Scoped".to_owned();
let anchor = RwAnchor::new(&mut x);
let portal = anchor.portal();
let _guard = portal.borrow();
assert_deadlock!(drop(anchor), Duration::from_secs(1));
§Panics
On drop, if any associated RwPortal
s exist:
use ref_portals::rc::RwAnchor;
let mut x = "Scoped".to_owned();
let anchor = RwAnchor::new(&mut x);
Box::leak(Box::new(anchor.portal()));
assert_panic!(
drop(anchor),
&str,
"Anchor still in use (at least one portal exists)",
);
Otherwise, on drop, iff the anchor has been poisoned:
use ref_portals::rc::RwAnchor;
let mut x = "Scoped".to_owned();
let anchor = RwAnchor::new(&mut x);
{
let portal = anchor.portal();
assert_panic!({
let guard = portal.borrow_mut();
panic!()
});
}
assert_panic!(
drop(anchor),
&str,
"Anchor poisoned",
);
Implementations§
Source§impl<'a, T: ?Sized> RwAnchor<'a, T>
impl<'a, T: ?Sized> RwAnchor<'a, T>
Sourcepub fn portal(&self) -> RwPortal<T>
pub fn portal(&self) -> RwPortal<T>
Creates a fallible portal with unbounded lifetime supporting overlapping reads.
§Example
let mut x = "Scoped".to_owned();
let anchor = RwAnchor::new(&mut x);
let self_owned: Box<dyn Fn() + 'static> = Box::new({
let portal = anchor.portal();
move || {
println!("{}", *portal.borrow());
*portal.borrow_mut() = "Replacement".to_owned();
}
});
self_owned(); // Scoped
drop(self_owned);
drop(anchor);
println!("{}", x); // Replacement
pub fn weak_portal(&self) -> WeakRwPortal<T>
Trait Implementations§
Source§impl<'a, T: ?Sized> Drop for RwAnchor<'a, T>
impl<'a, T: ?Sized> Drop for RwAnchor<'a, T>
Source§fn drop(&mut self)
fn drop(&mut self)
Executes the destructor for this type. Read more
§Panics
If any associated RwPortal
s exist or, otherwise, iff the anchor has been poisoned:
use ref_portals::rc::RwAnchor;
let mut x = "Scoped".to_owned();
let anchor = RwAnchor::new(&mut x);
let portal = anchor.portal();
assert_panic!({
// Poison anchor.
let _guard = portal.borrow_mut();
panic!()
});
assert_panic!(
drop(anchor),
&str,
"Anchor still in use (at least one portal exists)",
);
impl<'a, T> UnwindSafe for RwAnchor<'a, T>where
T: RefUnwindSafe + ?Sized,
§Safety:
use ref_portals::rc::RwAnchor;
let mut x = "Scoped".to_owned();
let anchor = RwAnchor::new(&mut x);
let portal = anchor.portal();
assert_panic!(
drop(anchor),
&str,
"Anchor still in use (at least one portal exists)",
);
assert_panic!(
{ portal.borrow_mut(); },
&str,
"Anchor poisoned",
);
Auto Trait Implementations§
impl<'a, T> Freeze for RwAnchor<'a, T>where
T: ?Sized,
impl<'a, T> !RefUnwindSafe for RwAnchor<'a, T>
impl<'a, T> !Send for RwAnchor<'a, T>
impl<'a, T> !Sync for RwAnchor<'a, T>
impl<'a, T> Unpin for RwAnchor<'a, T>where
T: ?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
Mutably borrows from an owned value. Read more
Source§impl<T> FmtForward for T
impl<T> FmtForward for T
Source§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
Causes
self
to use its Binary
implementation when Debug
-formatted.Source§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
Causes
self
to use its Display
implementation when
Debug
-formatted.Source§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
Causes
self
to use its LowerExp
implementation when
Debug
-formatted.Source§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
Causes
self
to use its LowerHex
implementation when
Debug
-formatted.Source§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
Causes
self
to use its Octal
implementation when Debug
-formatted.Source§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
Causes
self
to use its Pointer
implementation when
Debug
-formatted.Source§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
Causes
self
to use its UpperExp
implementation when
Debug
-formatted.Source§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
Causes
self
to use its UpperHex
implementation when
Debug
-formatted.Source§impl<T> PipeAsRef for T
impl<T> PipeAsRef for T
Source§impl<T> PipeBorrow for T
impl<T> PipeBorrow for T
Source§impl<T> PipeDeref for T
impl<T> PipeDeref for T
Source§impl<T> PipeRef for T
impl<T> PipeRef for T
Source§impl<T> Tap for T
impl<T> Tap for T
Source§fn tap<F, R>(self, func: F) -> Selfwhere
F: FnOnce(&Self) -> R,
fn tap<F, R>(self, func: F) -> Selfwhere
F: FnOnce(&Self) -> R,
Provides immutable access for inspection. Read more
Source§fn tap_dbg<F, R>(self, func: F) -> Selfwhere
F: FnOnce(&Self) -> R,
fn tap_dbg<F, R>(self, func: F) -> Selfwhere
F: FnOnce(&Self) -> R,
Calls
tap
in debug builds, and does nothing in release builds.Source§fn tap_mut<F, R>(self, func: F) -> Selfwhere
F: FnOnce(&mut Self) -> R,
fn tap_mut<F, R>(self, func: F) -> Selfwhere
F: FnOnce(&mut Self) -> R,
Provides mutable access for modification. Read more
Source§fn tap_mut_dbg<F, R>(self, func: F) -> Selfwhere
F: FnOnce(&mut Self) -> R,
fn tap_mut_dbg<F, R>(self, func: F) -> Selfwhere
F: FnOnce(&mut Self) -> R,
Calls
tap_mut
in debug builds, and does nothing in release builds.Source§impl<T, U> TapAsRef<U> for Twhere
U: ?Sized,
impl<T, U> TapAsRef<U> for Twhere
U: ?Sized,
Source§fn tap_ref<F, R>(self, func: F) -> Self
fn tap_ref<F, R>(self, func: F) -> Self
Provides immutable access to the reference for inspection.
Source§fn tap_ref_dbg<F, R>(self, func: F) -> Self
fn tap_ref_dbg<F, R>(self, func: F) -> Self
Calls
tap_ref
in debug builds, and does nothing in release builds.Source§fn tap_ref_mut<F, R>(self, func: F) -> Self
fn tap_ref_mut<F, R>(self, func: F) -> Self
Provides mutable access to the reference for modification.
Source§impl<T, U> TapBorrow<U> for Twhere
U: ?Sized,
impl<T, U> TapBorrow<U> for Twhere
U: ?Sized,
Source§fn tap_borrow<F, R>(self, func: F) -> Self
fn tap_borrow<F, R>(self, func: F) -> Self
Provides immutable access to the borrow for inspection. Read more
Source§fn tap_borrow_dbg<F, R>(self, func: F) -> Self
fn tap_borrow_dbg<F, R>(self, func: F) -> Self
Calls
tap_borrow
in debug builds, and does nothing in release builds.Source§fn tap_borrow_mut<F, R>(self, func: F) -> Self
fn tap_borrow_mut<F, R>(self, func: F) -> Self
Provides mutable access to the borrow for modification.
Source§impl<T> TapDeref for T
impl<T> TapDeref for T
Source§fn tap_deref_dbg<F, R>(self, func: F) -> Self
fn tap_deref_dbg<F, R>(self, func: F) -> Self
Calls
tap_deref
in debug builds, and does nothing in release builds.Source§fn tap_deref_mut<F, R>(self, func: F) -> Self
fn tap_deref_mut<F, R>(self, func: F) -> Self
Mutably dereferences
self
for modification.