Struct TakeAndReplace

Source
pub struct TakeAndReplace<'a, T, U, F>
where F: Fn(&mut T) -> &mut U, U: Default,
{ /* private fields */ }
Expand description

An RAII type to temporarily take a U out of a T and then put it back again on drop.

This allows you to split borrows, if necessary, to satisfy the borrow checker.

The F type parameter must project from the container type T to its U that we want to temporarily take out of it.

§Example

use cranelift_codegen::TakeAndReplace;

#[derive(Default)]
struct BigContextStruct {
    items: Vec<u32>,
    count: usize,
}

impl BigContextStruct {
    fn handle_item(&mut self, item: u32) {
        self.count += 1;
        println!("Handled {item}!");
    }
}

let mut ctx = BigContextStruct::default();
ctx.items.extend([42, 1337, 1312]);

{
    // Temporarily take `self.items` out of `ctx`.
    let mut guard = TakeAndReplace::new(&mut ctx, |ctx| &mut ctx.items);
    let (ctx, items) = guard.get();

    // Now we can both borrow/iterate/mutate `items` and call `&mut self` helper
    // methods on `ctx`. This would not otherwise be possible if we didn't split
    // the borrows, since Rust's borrow checker doesn't see through methods and
    // know that `handle_item` doesn't use `self.items`.
    for item in items.drain(..) {
        ctx.handle_item(item);
    }
}

// When `guard` is dropped, `items` is replaced in `ctx`, allowing us to
// reuse its capacity and avoid future allocations.  ```
assert!(ctx.items.capacity() >= 3);

Implementations§

Source§

impl<'a, T, U, F> TakeAndReplace<'a, T, U, F>
where F: Fn(&mut T) -> &mut U, U: Default,

Source

pub fn new(container: &'a mut T, proj: F) -> Self

Create a new TakeAndReplace that temporarily takes out proj(container).

Source

pub fn get(&mut self) -> (&mut T, &mut U)

Get the underlying container and taken-out value.

Trait Implementations§

Source§

impl<'a, T, U, F> Drop for TakeAndReplace<'a, T, U, F>
where F: Fn(&mut T) -> &mut U, U: Default,

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<'a, T, U, F> Freeze for TakeAndReplace<'a, T, U, F>
where U: Freeze, F: Freeze,

§

impl<'a, T, U, F> RefUnwindSafe for TakeAndReplace<'a, T, U, F>

§

impl<'a, T, U, F> Send for TakeAndReplace<'a, T, U, F>
where U: Send, F: Send, T: Send,

§

impl<'a, T, U, F> Sync for TakeAndReplace<'a, T, U, F>
where U: Sync, F: Sync, T: Sync,

§

impl<'a, T, U, F> Unpin for TakeAndReplace<'a, T, U, F>
where U: Unpin, F: Unpin,

§

impl<'a, T, U, F> !UnwindSafe for TakeAndReplace<'a, T, U, F>

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> Same for T

Source§

type Output = T

Should always be Self
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.