ref_wrapper/
lib.rs

1/*! Wrapper of dynamically borrowed data.
2
3*The author of this crate is not good at English.*
4*Forgive me if the document is hard to read.*
5
6This crate provide wrappers for value generated from dynamic borrowing types.
7
8| Wrapper        | Target             |
9|----------------|--------------------|
10| [`RefWrap`]    | [`Ref`][Ref]       |
11| [`RefWrapMut`] | [`RefMut`][RefMut] |
12
13For example, this makes it possible to generate iterators from dynamicaly
14borrowed values. Note that the original borrowing is consumed and taken into
15the wrapper type. Therefore, when the wrapper is dropped, the original
16borrowing is also dropped.
17
18[Ref]: core::cell::Ref
19[RefMut]: core::cell::RefMut
20
21# Examples
22
23Normal use case.
24
25```
26# use ref_wrapper::{RefWrap};
27# use core::cell::{Ref, RefCell};
28# use core::ops::Deref;
29#
30let src = RefCell::new(vec![1, 2, 3]);
31let target = RefWrap::new(src.borrow(), |x| VecStat(x));
32assert_eq!(target.summary(), 6);
33
34pub struct VecStat<'a>(&'a Vec<i32>);
35impl<'a> VecStat<'a> {
36    pub fn summary(&self) -> i32 {
37        self.0.iter().sum::<i32>()
38    }
39}
40```
41
42Iterator use case.
43
44```
45# use ref_wrapper::RefWrap;
46# use core::cell::{Ref, RefCell};
47#
48let src = RefCell::new(vec![1, 2, 3]);
49let iter = RefWrap::new(src.borrow(), |x| x.iter());
50assert_eq!(iter.sum::<i32>(), 6);
51```
52*/
53
54#![no_std]
55
56mod impl_iterator;
57mod ref_wrap;
58mod ref_wrap_mut;
59mod reset_lifetime;
60
61pub use ref_wrap::*;
62pub use ref_wrap_mut::*;
63use reset_lifetime::*;