Loaned

Struct Loaned 

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

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

Thus, for the duration of 't, one cannot mutably access this value. However, unlike LoanedMut, one can immutably access it.

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

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

§Dropping

The value held by a Loaned 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 Loaned panics.

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

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

Implementations§

Source§

impl<'t, T> Loaned<'t, T>

Source

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

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

Source

pub fn new(value: T) -> Self

Creates a Loaned without actually loaning it. If you want to loan it, use Loaned::loan or Loaned::borrow.

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.

Source

pub fn borrow(&self) -> &'t T::Target
where T: Loanable<'t>,

Borrows the pointee of the value, returning a reference valid for 't.

Source§

impl<'t, T> Loaned<'t, T>

Source

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

Merges multiple LoanedMut values.

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

impl<'t, T> Loaned<'t, T>

Source

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

Creates a Loaned with multiple sub-loans.

§Example
use loaned::Loaned;
let ((a, b), ab) = Loaned::loan_with((Box::new(1), Box::new(2)), |ab, l| {
  (l.loan(&ab.0), l.loan(&ab.1))
});
assert_eq!(*a, 1);
assert_eq!(*b, 2);
assert_eq!(loaned::take!(ab), (Box::new(1), Box::new(2)));

Trait Implementations§

Source§

impl<'t, T: Clone> Clone for Loaned<'t, T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'t, T: Debug> Debug for Loaned<'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 Loaned<'t, T>

Source§

fn default() -> Self

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

impl<'t, T> Deref for Loaned<'t, T>

Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &T

Dereferences the value.
Source§

impl<'t, T: Display> Display for Loaned<'t, T>

Source§

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

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

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

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

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

Source§

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

Converts to this type from the input type.
Source§

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

Source§

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

Converts to this type from the input type.
Source§

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

Source§

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

Converts to this type from the input type.
Source§

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

Source§

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

Converts to this type from the input type.
Source§

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

Source§

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

Converts to this type from the input type.
Source§

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

Source§

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

Converts to this type from the input type.
Source§

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

Source§

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

Converts to this type from the input type.
Source§

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

Source§

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

Converts to this type from the input type.
Source§

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

Source§

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

Converts to this type from the input type.
Source§

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

Source§

fn from( value: (Loaned<'t, A>, Loaned<'t, B>, Loaned<'t, C>, Loaned<'t, D>, Loaned<'t, E>, Loaned<'t, F>, Loaned<'t, G>, Loaned<'t, H>, Loaned<'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<(Loaned<'t, A>, Loaned<'t, B>, Loaned<'t, C>, Loaned<'t, D>, Loaned<'t, E>, Loaned<'t, F>, Loaned<'t, G>, Loaned<'t, H>, Loaned<'t, I>, Loaned<'t, J>)> for Loaned<'t, (A, B, C, D, E, F, G, H, I, J)>

Source§

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

Converts to this type from the input type.
Source§

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

Available on crate feature alloc only.
Source§

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

Converts to this type from the input type.
Source§

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

Source§

fn from(value: Loaned<'t, MaybeUninit<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<MaybeUninit<Loaned<'t, T>>> for Loaned<'t, MaybeUninit<T>>

Source§

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

Converts to this type from the input type.
Source§

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

Source§

fn from(value: T) -> Self

Converts to this type from the input type.
Source§

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

Available on crate feature alloc only.
Source§

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

Converts to this type from the input type.
Source§

impl<'t, T: Hash> Hash for Loaned<'t, T>

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<'t, T: Ord> Ord for Loaned<'t, T>

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<'t, 'u, T: PartialEq<U>, U> PartialEq<Loaned<'u, U>> for Loaned<'t, T>

Source§

fn eq(&self, other: &Loaned<'u, U>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'t, 'u, T: PartialOrd<U>, U> PartialOrd<Loaned<'u, U>> for Loaned<'t, T>

Source§

fn partial_cmp(&self, other: &Loaned<'u, U>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

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

Source§

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

Source§

impl<'t, T: Eq> Eq for Loaned<'t, T>

Source§

impl<'t, T: Sync> Send for Loaned<'t, T>

Like &T, Loaned<T> is only Send if T is Sync.

Otherwise, code could cause data races:

use loaned::Loaned;
use std::cell::Cell;
let x = Loaned::new(Box::new(Cell::new(123)));
let y = x.borrow();
let x = std::thread::scope(|s| {
  let h = s.spawn(move || {
    x.set(456); // <- unsynchronized write
    x
  });
  y.get(); // // <- unsynchronized read
  h.join().unwrap()
});
loaned::drop!(x);

If you need to safely send this value, you can convert it to a LoanedMut<'t, T> with Into.

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

impl<'t, T> UnwindSafe for Loaned<'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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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.