Struct LoanedMut

Source
pub struct LoanedMut<'t, T> { /* private fields */ }
Expand description

LoanedMut<'t, T> connotes ownership of a value T, with the caveat that allocations owned by it are mutably loaned for 't (i.e. something else may hold an &'t mut reference to such allocations).

Thus, for the duration of 't, one cannot access this value.

One can, however, store this value somewhere with LoanedMut::place, which will ensure that it cannot be used for the duration of 't.

Taking the value out of a LoanedMut can be done with the take! macro, which will statically ensure that 't has expired.

§Dropping

The value held by a LoanedMut can only be dropped once 't expires. Since there is no way in the type system to enforce this, nor any way to check this at runtime, dropping a LoanedMut panics.

If leaking is intentional, use a ManuallyDrop<LoanedMut<'t, T>>.

To drop the inner value, use the drop! macro, which will statically ensure that 't has expired.

Implementations§

Source§

impl<'t, T> LoanedMut<'t, T>

Source

pub fn loan(value: T) -> (&'t mut T::Target, Self)
where T: Loanable<'t> + DerefMut,

Constructs a LoanedMut from a given smart pointer, returning the mutable borrow along with the loaned pointer.

Examples found in repository?
examples/safe_arena.rs (line 33)
29  fn new_chunk(capacity: usize) -> (&'t mut [Option<T>], LoanedMut<'t, Box<[Option<T>]>>) {
30    let mut chunk = Vec::with_capacity(capacity);
31    chunk.resize_with(capacity, || None);
32    let chunk = chunk.into_boxed_slice();
33    let (cursor, chunk) = LoanedMut::loan(chunk);
34    (cursor, chunk)
35  }
Source

pub fn new(value: T) -> Self

Creates a LoanedMut without actually loaning it. If you want to loan it, use LoanedMut::loan.

Source

pub fn place(self, place: &'t mut impl Place<'t, T>)

Stores the contained value into a given place. See the Place trait for more.

Examples found in repository?
examples/tree_building.rs (line 55)
12fn main() {
13  // First, we create a single node `a`, which has two holes, `b` and `c`.
14  // ```text
15  //    a
16  //   / \
17  // ?b   ?c
18  // ```
19  // `a` is a `LoanedMut<Tree>`, whilst `b` and `c` are `&mut Tree`s.
20  let (a, b, c) = new_node();
21
22  // Next, we fill one of those holes, `b`, with a leaf:
23  // ```text
24  //    a
25  //   / \
26  //  1   ?c
27  // ```
28  *b = Tree::Leaf(1);
29
30  // Now, we create another node, `x`, with holes `y` and `z`:
31  // ```text
32  //   a        x
33  //  / \      / \
34  // 1   ?c  ?y   ?z
35  // ```
36  let (x, y, z) = new_node();
37
38  // We fill `y` with another leaf:
39  // ```text
40  //   a        x
41  //  / \      / \
42  // 1   ?c   2   ?z
43  // ```
44  *y = Tree::Leaf(2);
45
46  // Now, we fill the hole `c` with the node `x`.
47  // ```text
48  //   a
49  //  / \
50  // 1  / \
51  //   2   ?z
52  // ```
53  // We still have a mutable reference to the hole `z`, even though we just
54  // moved ownership of `x` – this is the key power of `Loaned` values.
55  x.place(c); // this is like `*c = x`, except it accepts `Loaned` values.
56
57  // Finally, we can fill the hole `z` (which is a mutable reference to data now owned by `a`):
58  // ```text
59  //   a
60  //  / \
61  // 1  / \
62  //   2   3
63  // ```
64  *z = Tree::Leaf(3);
65
66  // All of the borrows have expired, so we can now take the tree out of `a`:
67  let a = take!(a);
68
69  println!("{a:?}");
70  assert_eq!(format!("{a:?}"), "Node(Leaf(1), Node(Leaf(2), Leaf(3)))");
71
72  // If we tried to use one of the borrows now, we would get an error from the borrow checker.
73  // *z = Tree::Leaf(0xBAD);
74}
Source§

impl<'t, T> LoanedMut<'t, T>

Source

pub fn merge( value: T, f: impl for<'i> FnOnce(&'i mut T, &'i MergeMut<'t, 'i>), ) -> Self

Merges multiple LoanedMut values.

§Example
use loaned::LoanedMut;
let a = LoanedMut::new(1);
let b = LoanedMut::new(2);
let ab: LoanedMut<(u32, u32)> = LoanedMut::merge(Default::default(), |ab, m| {
  m.place(a, &mut ab.0);
  m.place(b, &mut ab.1);
});
Source§

impl<'t, T> LoanedMut<'t, T>

Source

pub fn loan_with<L>( value: T, f: impl for<'i> FnOnce(&'i mut T, &'i LoanWithMut<'t, 'i>) -> L, ) -> (L, Self)

Creates a LoanedMut with multiple sub-loans.

§Example
use loaned::LoanedMut;
let ((a, b), ab) = LoanedMut::loan_with((Box::new(0), Box::new(0)), |ab, l| {
  (l.loan_mut(&mut ab.0), l.loan_mut(&mut ab.1))
});
*a = 1;
*b = 2;
assert_eq!(loaned::take!(ab), (Box::new(1), Box::new(2)));
Examples found in repository?
examples/tree_building.rs (lines 77-85)
76fn new_node<'t>() -> (LoanedMut<'t, Tree>, &'t mut Tree, &'t mut Tree) {
77  let ((left, right), root) = LoanedMut::loan_with(
78    Tree::Node(Default::default(), Default::default()),
79    |tree, l| {
80      let Tree::Node(left, right) = tree else {
81        unreachable!()
82      };
83      (l.loan_mut(left), l.loan_mut(right))
84    },
85  );
86  (root, left, right)
87}

Trait Implementations§

Source§

impl<'t, T> Debug for LoanedMut<'t, T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'t, T: Default> Default for LoanedMut<'t, T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'t, T> Drop for LoanedMut<'t, T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<'t, T, const N: usize> From<[LoanedMut<'t, T>; N]> for LoanedMut<'t, [T; N]>

Source§

fn from(value: [LoanedMut<'t, T>; N]) -> Self

Converts to this type from the input type.
Source§

impl<'t, A> From<(LoanedMut<'t, A>,)> for LoanedMut<'t, (A,)>

Source§

fn from(value: (LoanedMut<'t, A>,)) -> Self

Converts to this type from the input type.
Source§

impl<'t, A, B> From<(LoanedMut<'t, A>, LoanedMut<'t, B>)> for LoanedMut<'t, (A, B)>

Source§

fn from(value: (LoanedMut<'t, A>, LoanedMut<'t, B>)) -> Self

Converts to this type from the input type.
Source§

impl<'t, A, B, C> From<(LoanedMut<'t, A>, LoanedMut<'t, B>, LoanedMut<'t, C>)> for LoanedMut<'t, (A, B, C)>

Source§

fn from(value: (LoanedMut<'t, A>, LoanedMut<'t, B>, LoanedMut<'t, C>)) -> Self

Converts to this type from the input type.
Source§

impl<'t, A, B, C, D> From<(LoanedMut<'t, A>, LoanedMut<'t, B>, LoanedMut<'t, C>, LoanedMut<'t, D>)> for LoanedMut<'t, (A, B, C, D)>

Source§

fn from( value: (LoanedMut<'t, A>, LoanedMut<'t, B>, LoanedMut<'t, C>, LoanedMut<'t, D>), ) -> Self

Converts to this type from the input type.
Source§

impl<'t, A, B, C, D, E> From<(LoanedMut<'t, A>, LoanedMut<'t, B>, LoanedMut<'t, C>, LoanedMut<'t, D>, LoanedMut<'t, E>)> for LoanedMut<'t, (A, B, C, D, E)>

Source§

fn from( value: (LoanedMut<'t, A>, LoanedMut<'t, B>, LoanedMut<'t, C>, LoanedMut<'t, D>, LoanedMut<'t, E>), ) -> Self

Converts to this type from the input type.
Source§

impl<'t, A, B, C, D, E, F> From<(LoanedMut<'t, A>, LoanedMut<'t, B>, LoanedMut<'t, C>, LoanedMut<'t, D>, LoanedMut<'t, E>, LoanedMut<'t, F>)> for LoanedMut<'t, (A, B, C, D, E, F)>

Source§

fn from( value: (LoanedMut<'t, A>, LoanedMut<'t, B>, LoanedMut<'t, C>, LoanedMut<'t, D>, LoanedMut<'t, E>, LoanedMut<'t, F>), ) -> Self

Converts to this type from the input type.
Source§

impl<'t, A, B, C, D, E, F, G> From<(LoanedMut<'t, A>, LoanedMut<'t, B>, LoanedMut<'t, C>, LoanedMut<'t, D>, LoanedMut<'t, E>, LoanedMut<'t, F>, LoanedMut<'t, G>)> for LoanedMut<'t, (A, B, C, D, E, F, G)>

Source§

fn from( value: (LoanedMut<'t, A>, LoanedMut<'t, B>, LoanedMut<'t, C>, LoanedMut<'t, D>, LoanedMut<'t, E>, LoanedMut<'t, F>, LoanedMut<'t, G>), ) -> Self

Converts to this type from the input type.
Source§

impl<'t, A, B, C, D, E, F, G, H> From<(LoanedMut<'t, A>, LoanedMut<'t, B>, LoanedMut<'t, C>, LoanedMut<'t, D>, LoanedMut<'t, E>, LoanedMut<'t, F>, LoanedMut<'t, G>, LoanedMut<'t, H>)> for LoanedMut<'t, (A, B, C, D, E, F, G, H)>

Source§

fn from( value: (LoanedMut<'t, A>, LoanedMut<'t, B>, LoanedMut<'t, C>, LoanedMut<'t, D>, LoanedMut<'t, E>, LoanedMut<'t, F>, LoanedMut<'t, G>, LoanedMut<'t, H>), ) -> Self

Converts to this type from the input type.
Source§

impl<'t, A, B, C, D, E, F, G, H, I> From<(LoanedMut<'t, A>, LoanedMut<'t, B>, LoanedMut<'t, C>, LoanedMut<'t, D>, LoanedMut<'t, E>, LoanedMut<'t, F>, LoanedMut<'t, G>, LoanedMut<'t, H>, LoanedMut<'t, I>)> for LoanedMut<'t, (A, B, C, D, E, F, G, H, I)>

Source§

fn from( value: (LoanedMut<'t, A>, LoanedMut<'t, B>, LoanedMut<'t, C>, LoanedMut<'t, D>, LoanedMut<'t, E>, LoanedMut<'t, F>, LoanedMut<'t, G>, LoanedMut<'t, H>, LoanedMut<'t, I>), ) -> Self

Converts to this type from the input type.
Source§

impl<'t, A, B, C, D, E, F, G, H, I, J> From<(LoanedMut<'t, A>, LoanedMut<'t, B>, LoanedMut<'t, C>, LoanedMut<'t, D>, LoanedMut<'t, E>, LoanedMut<'t, F>, LoanedMut<'t, G>, LoanedMut<'t, H>, LoanedMut<'t, I>, LoanedMut<'t, J>)> for LoanedMut<'t, (A, B, C, D, E, F, G, H, I, J)>

Source§

fn from( value: (LoanedMut<'t, A>, LoanedMut<'t, B>, LoanedMut<'t, C>, LoanedMut<'t, D>, LoanedMut<'t, E>, LoanedMut<'t, F>, LoanedMut<'t, G>, LoanedMut<'t, H>, LoanedMut<'t, I>, LoanedMut<'t, J>), ) -> Self

Converts to this type from the input type.
Source§

impl<'t, T> From<Box<LoanedMut<'t, T>>> for LoanedMut<'t, Box<T>>

Source§

fn from(value: Box<LoanedMut<'t, T>>) -> Self

Converts to this type from the input type.
Source§

impl<'t, T> From<Loaned<'t, T>> for LoanedMut<'t, T>

Source§

fn from(value: Loaned<'t, T>) -> Self

Converts to this type from the input type.
Source§

impl<'t, T> From<LoanedMut<'t, MaybeUninit<T>>> for MaybeUninit<LoanedMut<'t, T>>

Source§

fn from(value: LoanedMut<'t, MaybeUninit<T>>) -> Self

Converts to this type from the input type.
Source§

impl<'t, T> From<MaybeUninit<LoanedMut<'t, T>>> for LoanedMut<'t, MaybeUninit<T>>

Source§

fn from(value: MaybeUninit<LoanedMut<'t, T>>) -> Self

Converts to this type from the input type.
Source§

impl<'t, T> From<T> for LoanedMut<'t, T>

Source§

fn from(value: T) -> Self

Converts to this type from the input type.
Source§

impl<'t, T> From<Vec<LoanedMut<'t, T>>> for LoanedMut<'t, Vec<T>>

Source§

fn from(value: Vec<LoanedMut<'t, T>>) -> Self

Converts to this type from the input type.
Source§

impl<'t, T> Placeable<'t, T> for LoanedMut<'t, T>

Source§

fn place(self, place: &'t mut impl Place<'t, T>)

Auto Trait Implementations§

§

impl<'t, T> Freeze for LoanedMut<'t, T>
where T: Freeze,

§

impl<'t, T> RefUnwindSafe for LoanedMut<'t, T>
where T: RefUnwindSafe,

§

impl<'t, T> Send for LoanedMut<'t, T>
where T: Send,

§

impl<'t, T> Sync for LoanedMut<'t, T>
where T: Sync,

§

impl<'t, T> Unpin for LoanedMut<'t, T>
where T: Unpin,

§

impl<'t, T> UnwindSafe for LoanedMut<'t, T>
where T: 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<!> for T

Source§

fn from(t: !) -> T

Converts to this type from the input type.
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, T> Place<'t, T> for T

Source§

fn place(loaned: LoanedMut<'t, T>, place: &'t mut 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.