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),

source

pub fn new(value: Value, dropper: DropFn) -> Self

Create a wrapped value

source

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),

source§

type Target = Value

The resulting type after dereferencing.
source§

fn deref(&self) -> &Self::Target

Dereferences the value.
source§

impl<DropFn, Value> DerefMut for ExplicitDropper<DropFn, Value>
where DropFn: FnOnce(Value),

source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
source§

impl<DropFn, Value> Drop for ExplicitDropper<DropFn, Value>
where DropFn: FnOnce(Value),

source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<DropFn, Value> Freeze for ExplicitDropper<DropFn, Value>
where DropFn: Freeze, Value: Freeze,

§

impl<DropFn, Value> RefUnwindSafe for ExplicitDropper<DropFn, Value>
where DropFn: RefUnwindSafe, Value: RefUnwindSafe,

§

impl<DropFn, Value> Send for ExplicitDropper<DropFn, Value>
where DropFn: Send, Value: Send,

§

impl<DropFn, Value> Sync for ExplicitDropper<DropFn, Value>
where DropFn: Sync, Value: Sync,

§

impl<DropFn, Value> Unpin for ExplicitDropper<DropFn, Value>
where DropFn: Unpin, Value: Unpin,

§

impl<DropFn, Value> UnwindSafe for ExplicitDropper<DropFn, Value>
where DropFn: UnwindSafe, Value: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> NoTypeInference for T

source§

type This = T

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

source§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V