Expand description
Wrapper of dynamically borrowed data.
The author of this crate is not good at English. Forgive me if the document is hard to read.
This crate provide wrappers for value generated from dynamic borrowing types.
Wrapper | Target |
---|---|
RefWrap | Ref |
RefWrapMut | RefMut |
For example, this makes it possible to generate iterators from dynamicaly borrowed values. Note that the original borrowing is consumed and taken into the wrapper type. Therefore, when the wrapper is dropped, the original borrowing is also dropped.
§Examples
Normal use case.
let src = RefCell::new(vec![1, 2, 3]);
let target = RefWrap::new(src.borrow(), |x| VecStat(x));
assert_eq!(target.summary(), 6);
pub struct VecStat<'a>(&'a Vec<i32>);
impl<'a> VecStat<'a> {
pub fn summary(&self) -> i32 {
self.0.iter().sum::<i32>()
}
}
Iterator use case.
let src = RefCell::new(vec![1, 2, 3]);
let iter = RefWrap::new(src.borrow(), |x| x.iter());
assert_eq!(iter.sum::<i32>(), 6);
Structs§
- RefWrap
- Wrapper of
Ref
. - RefWrap
Mut - Wrapper of
RefMut
.