Partial struct borrowing made easy, including splitting borrows.
```rust use std::borrow::Borrow; use borrow_as::*;
struct X {
s: String,
v: Vec
impl Default for X { fn default() -> Self { Self { s: String::from("No string for you"), v: vec![1, 2, 3], i: 0, x: 9, f: Box::new(|| 0), } } }
impl X {
fn constructa<'a>(s: &'a String,
v: &'a Vec
fn construct_b<'a>(i: &'a mut i8,
x: u32,
f: &'a (dyn Fn() -> i32 + 'static))
-> LifeRef<'a, B> {
LifeRef::
wrap_mut(i)
.add_ref(f)
.map_life(|(i, f)| B { i, x, f })
}
pub fn get_a(&self) -> LifeRef<'_, A> {
let Self { s, v, .. } = self;
Self::construct_a(s, v)
}
pub fn get_b(&mut self) -> LifeRef<'_, B> {
let Self { i, x, .. } = self;
Self::construct_b(i, *x, self.f.as_ref())
}
pub fn get_ab(&mut self) -> LifeRef<'_, (A, B)> {
let Self { s, v, i, x, .. } = self;
let a = Self::construct_a(s, v);
let b = Self::construct_b(i, *x, self.f.as_ref());
a.wrap_life().add_life(b)
}
pub fn get_c(&mut self) -> LifeRef<'_, C> {
let Self { f, v, i, .. } = self;
LifeRef::
wrap_mut(f)
.add_mut(v.as_mut_slice())
.add_ref(i)
.map_life(|(f, v, i)| C { f, v, i })
}
}
struct A {
pub s: Ref
pub struct B {
pub i: Mut
pub struct C {
pub v: Mut<[u128]>,
pub i: Ref
let mut x = X::default();
let a = x.geta(); asserteq!(a.s, "No string for you"); assert_eq!(a.v, [1, 2, 3]);
let b = x.getb(); let b: &B = b.borrow(); asserteq!(b.i.get(), 0); asserteq!(b.x, 9); asserteq!((b.f)(), 0);
b.i.set(1); let c = x.getc(); let c: &C = c.borrow(); asserteq!(c.i, &1);
c.f.set(Box::new(|| { 8 })); let v = c.v.assliceofcells(); v[2].set(4); let ab = x.getab(); asserteq!(ab.0.v, [1, 2, 4]); asserteq!((ab.1.f)(), 8);
asserteq!(x.s, "No string for you"); asserteq!(x.v, [1, 2, 4]); asserteq!(x.i, 1); asserteq!(x.x, 9); assert_eq!((x.f)(), 8); ```