#self-reference #reference #macro #memory-safety

any_ref

To capture and move things that are NOT 'static with memory safety

4 releases

0.2.1 Jun 19, 2022
0.2.0 Jun 19, 2022
0.1.5 Jun 18, 2022
0.1.3 Apr 12, 2022

#5 in #self-reference

Download history 5/week @ 2024-04-01 57/week @ 2024-07-08

57 downloads per month

MIT license

15KB
154 lines

Any-ref-rs

📖 Documentation

documentation

❓ What is any_ref

any_ref is a crate that allows you to capture things that are not 'static with memory safety.

Here's a simple demo to see how it works.

// Create the type `ReturnVec` stands for `Vec<&'_ T>`
make_any_ref! {
    type ReturnVec<T: 'static> = for<'a> Vec<&'a T>;
}

let moved_ar;
{
    // This is the owner
    let num: Box<(u16, u16, u16, u16)> = Box::new((1, 2, 3, 4));

    // Initialize an `AnyRef`
    let ar: AnyRef<ReturnVec<u16>, _> = AnyRef::new(
        num, |x| vec![&x.0, &x.1, &x.2, &x.3]
    );

    // Move out of this scope
    moved_ar = ar;
}

// Read the reference of the value
assert_eq!(moved_ar.get(), &vec![&1, &2, &3, &4]);

📣 Compatibility Notice

Below the version of rustc 1.61, due to a problem with type inferer, we cannot use AnyRef::new to initialize AnyRef in stable rust, using new_any_ref as an temporary alternative solution. Please upgrade your rustc to 1.61.0 or later if possible!

🤔 Troubleshooting

Everyone is welcomed to find out bugs or put forward your ideas of improvement, and please feel free to open an issue if you have any questions.

This crate is maintained (Written in 2022/6/19).

⚖️ License

MIT

❤️ Acknowledgements

Thanks to people in the rust forum who helped me find out serveral problems and bugs, thanks for your effort!

Dependencies

~1.5MB
~35K SLoC