any_ref 0.1.0

Creating a bundle that can carry an owner and a struct borrows it
Documentation
any_ref-0.1.0 has been yanked.

Please take a look at owning_ref before using this crate, which is excellent and more suitable for simple usage. One of the shortages of owning_ref is that owning_ref can only keeps reference and cannot keeps arbitrary struct with lifetime annotation. any_ref is here to resolve this problem.

Pre-made types:

make_any_ref!(
pub type Reference<T:'static + ?Sized> = for<'a> &'a T;
pub type ReferenceMut<T:'static + ?Sized> = for<'a> &'a mut T;
);

Example

use any_ref::{make_any_ref, new_any_ref, Reference};

make_any_ref! {
pub type _ReturnStr=for<'a> &'a str;
pub(crate) type ReturnVec<T:'static> = for<'a> Vec<&'a T>;
type _ReturnPhantomData<T:'static,const U:usize> = for<'lifetime> std::marker::PhantomData<&'lifetime (T,)>;
}

let moved_ar;
{
let num = Box::new((1, 2, 3, 4));
let ar = new_any_ref::<ReturnVec<u16>, _, _>(num, |x| vec![&x.0, &x.1, &x.2, &x.3]);
// We cannot use this temporarily due to a compiler bug. Use `new_any_ref` instead.
// let ar = AnyRef::<ReturnVec<u16>, _>::new(num, |x| vec![&x.0, &x.1, &x.2, &x.3]);
moved_ar = ar; // Move out of this scope
}
assert_eq!(moved_ar.get(), &vec![&1, &2, &3, &4]);

let moved_ar;
{
let s = "hello world".to_string();
let ar = new_any_ref::<Reference<str>, _, _>(s, |x| &x[..5]);
moved_ar = ar;
}
assert_eq!(moved_ar.get(), &"hello");