Struct recursive_reference::RecRef[][src]

pub struct RecRef<'a, T: ?Sized> { /* fields omitted */ }
Expand description

A Recursive reference. This struct is used to allow recursively reborrowing mutable references in a dynamic but safe way.

Implementations

This function extends the RecRef one time. That means, if the current reference is current_ref: &mut T, then this call extends the RecRef with the new reference ref2: &mut T = func(current_ref). After this call, the RecRef will expose the new ref2, and current_ref will be frozen (As it is borrowed by ref2), until ref2 is popped off, unfreezing current_ref.

Safety:

The type ensures no leaking is possible, since func can’t guarantee that current_ref will live for any length of time, so it can’t leak it anywhere. It can only use current_ref inside the function, and use it in order to return ref2, which is the intended usage.

A different point of view is this: we have to borrow current_ref to func with the actual correct lifetime: the lifetime in which it is allowed to freeze current_ref in order to use ref2.

However, we don’t know yet what that lifetime is: it will be whatever amount of time passes until ref2 will be popped back, unfreezing current_ref. (and that lifetime can even be decided dynamically). Whatever lifetime 'freeze_time that turns out to be, the type of func should have been func: FnOnce(&'freeze_time mut T) -> &'freeze_time mut T.

Therefore, we require that func will be able to work with any value of 'freeze_time, so we are sure that the code would’ve worked correctly if we put the correct lifetime there. So that ensures the code is safe.

Another point of view is considering what other types we could have given to this function: If the type was just

fn extend<'a, F : FnOnce(&'a mut T) -> &'a mut T>(&mut self, func : F)

then this function would be unsafe, because func could leak the reference outside, and then the caller could immediately pop the RecRef to get another copy of the same reference.

We could use

fn extend<'a, F : FnOnce(&'a mut T) -> &'a mut T>(&'a mut self, func : F)

But that would invalidate the whole point of using the RecRef - You couldn’t use it after extending even once, and it couldn’t be any better than a regular mutable reference.

Same as Self::extend, but allows the function to return an error value.

Same as Self::extend, but allows the function to return an error value, and also tells the inner function that 'a : 'b using a phantom argument.

This function maps the top of the RecRef. It’s similar to Self::extend, but it replaces the current reference instead of keeping it. See Self::extend for more details.

Same as Self::map, but allows the function to return an error value.

Same as Self::map, but allows the function to return an error value, and also tells the inner function that 'a : 'b using a phantom argument.

Push another reference to the RecRef, unrelated to the current one. rec_ref.push(new_ref) is morally equivalent to rec_ref.extend_result_precise(move |_, _| { Ok(new_ref) }). However, you might have some trouble making the anonymous function conform to the right type.

Lets the user use the last reference for some time, and discards it completely. After the user uses it, the next time they inspect the RecRef, it won’t be there. Can’t pop the last reference, as the RecRef can’t be empty, and returns None instead.

Discards the RecRef and returns the last reference. The difference between this and using Self::pop are:

  • This will consume the RecRef
  • Self::pop will never pop the first original reference, because that would produce an invalid RecRef. Self::into_ref will.

Gets the std::ptr::NonNull pointer that is i’th from the top of the RecRef. The intended usage is for using the pointers as the inputs to ptr_eq. However, using the pointers themselves (which requires unsafe) will almost definitely break rust’s guarantees.

Trait Implementations

The resulting type after dereferencing.

Dereferences the value.

Mutably dereferences the value.

Performs the conversion.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.