noamtashma / owning-ref-rs

A library for creating references that carry their owner with them.
MIT License
7 stars 2 forks source link

Support arbitrary borrowers #5

Open Ddystopia opened 3 months ago

Ddystopia commented 3 months ago

Currently, for example map_mut has following signature:

pub fn map_mut<F, U>(self, f: F) -> OwningRefMut<O, U>
where
    U: ?Sized,
    O: StableAddress,
    F: FnOnce(&mut T) -> &mut U

which makes impossible view types like this (U + 'a also include &'a mut i32, so it is +- backwards compatible):

use owning_ref::OwningRefMut;

struct View<'a> {
    a: &'a mut i32,
    b: &'a mut i32,
}

fn main() {
    let vec = vec![1, 2, 3, 4, 5];
    let it = OwningRefMut::new(vec).map_mut(|v| {
        let (a, b) = v.split_at_mut(1);
        View { a: &mut a[0], b: &mut b[0] }
    });
}

Signature may become something like that:

pub fn map_mut<F, U>(self, f: F) -> OwningRefMut<O, U>
where
    U,
    O: StableAddress,
    F: FnOnce(&mut T) -> U 

I propose change api of methods in crate to allow such use cases.

I need this because I need a view type for later stages and creating view type every time is expensive, because it is hot

Ddystopia commented 3 months ago

There is crate yoke and they are doing pretty much the same, but only for immutable references

https://docs.rs/yoke