pub struct ExplicitDropper<DropFn, Value>where
DropFn: FnOnce(Value),{ /* private fields */ }
Expand description
Wrapper that uses a provided function to drop the inner value.
This is helpful for working with locals that must be explicitly dropped, without having to explicitly do so at every potential exit point of the current function.
fn validate_and_rc(root: &Root, s: String) -> Result<RootedRc<String>, Box<dyn Error>> {
let rc_string = ExplicitDropper::new(
RootedRc::new(root, s),
|value| value.explicit_drop(root));
if !rc_string.starts_with("x") {
// `ExplicitDropper` will call the provided closure, safely dropping the RootedRc.
return Err("bad prefix".into());
}
// We extract the value from the dropper at the point where we transfer ownership;
// in this case the closure is never called.
return Ok(rc_string.into_value())
}
Implementations§
source§impl<DropFn, Value> ExplicitDropper<DropFn, Value>where
DropFn: FnOnce(Value),
impl<DropFn, Value> ExplicitDropper<DropFn, Value>where
DropFn: FnOnce(Value),
sourcepub fn into_value(self) -> Value
pub fn into_value(self) -> Value
Unwrap the value, discarding the dropper.
Trait Implementations§
source§impl<DropFn, Value> Deref for ExplicitDropper<DropFn, Value>where
DropFn: FnOnce(Value),
impl<DropFn, Value> Deref for ExplicitDropper<DropFn, Value>where
DropFn: FnOnce(Value),
source§impl<DropFn, Value> DerefMut for ExplicitDropper<DropFn, Value>where
DropFn: FnOnce(Value),
impl<DropFn, Value> DerefMut for ExplicitDropper<DropFn, Value>where
DropFn: FnOnce(Value),
Auto Trait Implementations§
impl<DropFn, Value> Freeze for ExplicitDropper<DropFn, Value>
impl<DropFn, Value> RefUnwindSafe for ExplicitDropper<DropFn, Value>where
DropFn: RefUnwindSafe,
Value: RefUnwindSafe,
impl<DropFn, Value> Send for ExplicitDropper<DropFn, Value>
impl<DropFn, Value> Sync for ExplicitDropper<DropFn, Value>
impl<DropFn, Value> Unpin for ExplicitDropper<DropFn, Value>
impl<DropFn, Value> UnwindSafe for ExplicitDropper<DropFn, Value>where
DropFn: UnwindSafe,
Value: UnwindSafe,
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