Trait borrowme::BorrowMut

source ·
pub trait BorrowMut {
    type TargetMut<'a>
       where Self: 'a;

    // Required method
    fn borrow_mut(&mut self) -> Self::TargetMut<'_>;
}
Expand description

Borrow mutably from self.

This works similarly to BorrowMut but allows borrowing compoundly from &self by defining a generic TargetMut. This means it’s not just limited to returning an immediate reference to the borrowed value but can return something which receives lifetime parameters that borrows from self. This is called “compound borrowing” because it lets you return types which contains compound references.

It is recommended that you use borrow_mut instead of importing this trait.


What about std::borrow::BorrowMut?

The BorrowMut trait as defined can’t perform compound borrows from &mut self. Because the borrow_mut method immediately returns a reference to the borrowed type.

trait BorrowMut<Borrowed: ?Sized>: Borrow<Borrowed> {
    fn borrow_mut(&mut self) -> &Borrowed;
}

This means that there is no way to implement something like BorrowMut<Word<'a>> because it’s required that we return a reference which doesn’t outlive 'a, something that can’t be satisfied from the call to &mut self.

struct WordMut<'a>(&'a mut String);
struct OwnedWord(String);

impl<'a> BorrowMut<WordMut<'a>> for OwnedWord {
    fn borrow_mut(&mut self) -> &mut WordMut<'a> {
        &mut WordMut(&mut self.0)
    }
}
error[E0515]: cannot return reference to temporary value
 --> src/borrow.rs:37:9
  |
9 |         &mut WordMut(&mut self.0)
  |         ^------------------------
  |         ||
  |         |temporary value created here
  |         returns a reference to data owned by the current function

The solution implemented in this crate is to use a generic TargetMut, with which we can implement borrow_mut like this:

use borrowme::BorrowMut;

impl BorrowMut for OwnedWord {
    type TargetMut<'a> = WordMut<'a>;

    fn borrow_mut(&mut self) -> Self::TargetMut<'_> {
        WordMut(&mut self.0)
    }
}

A catch here is that BorrowMut can only be implemented once for each time, compared to BorrowMut<T>. But for our purposes this is fine. This crate is primarily intended to work with two symmetrical types and any deviation from that pattern can be handled by customizing the behavior of the #[borrowme] attribute.

Required Associated Types§

source

type TargetMut<'a> where Self: 'a

Required Methods§

source

fn borrow_mut(&mut self) -> Self::TargetMut<'_>

Borrow mutably from self.

Implementations on Foreign Types§

source§

impl<T> BorrowMut for LinkedList<T>where T: BorrowMut,

§

type TargetMut<'a> where T: 'a = LinkedList<<T as BorrowMut>::TargetMut<'a>, Global>

source§

fn borrow_mut(&mut self) -> Self::TargetMut<'_>

source§

impl<T> BorrowMut for Option<T>where T: BorrowMut,

§

type TargetMut<'a> where T: 'a = Option<<T as BorrowMut>::TargetMut<'a>>

source§

fn borrow_mut(&mut self) -> Self::TargetMut<'_>

source§

impl<T> BorrowMut for Vec<T>where T: BorrowMut,

§

type TargetMut<'a> where T: 'a = Vec<<T as BorrowMut>::TargetMut<'a>, Global>

source§

fn borrow_mut(&mut self) -> Self::TargetMut<'_>

source§

impl<K, V> BorrowMut for BTreeMap<K, V>where K: Borrow, V: BorrowMut, for<'a> K::Target<'a>: PartialOrd + Ord + Eq,

§

type TargetMut<'a> where K: 'a, V: 'a = BTreeMap<<K as Borrow>::Target<'a>, <V as BorrowMut>::TargetMut<'a>, Global>

source§

fn borrow_mut(&mut self) -> Self::TargetMut<'_>

source§

impl BorrowMut for String

§

type TargetMut<'a> = &'a mut String

source§

fn borrow_mut(&mut self) -> Self::TargetMut<'_>

source§

impl<K, V> BorrowMut for HashMap<K, V>where K: Borrow, V: BorrowMut, for<'a> K::Target<'a>: Hash + Eq,

§

type TargetMut<'a> where K: 'a, V: 'a = HashMap<<K as Borrow>::Target<'a>, <V as BorrowMut>::TargetMut<'a>, RandomState>

source§

fn borrow_mut(&mut self) -> Self::TargetMut<'_>

source§

impl<T> BorrowMut for [T]

§

type TargetMut<'a> where T: 'a = &'a mut [T]

source§

fn borrow_mut(&mut self) -> Self::TargetMut<'_>

Implementors§