std_util/
nested_ref.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
//! Tools for chaining borrows of [`std::cell::RefCell`].
//!
//! Useful for returning an abstract borrow, where multiple inner borrows are required. e.g.:
//!
//! ```
//! use std::cell::RefCell;
//!
//! struct MyPrivateType {
//!   x: RefCell<i32>
//! }
//!
//! pub struct MyPublicType {
//!   inner: RefCell<MyPrivateType>
//! }
//!
//! impl MyPublicType {
//!   pub fn borrow_x(&self) -> impl std::ops::Deref<Target=i32> + '_ {
//!     std_util::nested_ref::NestedRef::map(self.inner.borrow(), |inner| inner.x.borrow())
//!   }
//! }
//! ```
//!
//! Currently only supports nested RefCells; i.e. one level of nesting.
//!
//! TODO: It might be feasible to genericize the reference types, which would
//! also add support for arbitrary levels of nesting.
use std::cell::{Ref, RefMut};

/// A nested [`std::cell::Ref`]. Useful for chaining borrows with [`std::cell::RefCell`].
pub struct NestedRef<'a, Inner, Outer>
where
    Inner: 'static,
{
    // unsafely points to the Ref inside `_outer`.
    // Must be declared first so that it's dropped first.
    inner: Ref<'a, Inner>,
    // Boxed so that `Self` is movable without breaking `inner`'s reference.
    _outer: Box<Ref<'a, Outer>>,
}
impl<'a, Inner, Outer> NestedRef<'a, Inner, Outer>
where
    Inner: 'static,
{
    #[inline]
    pub fn map(outer: Ref<'a, Outer>, borrow_fn: impl FnOnce(&Outer) -> Ref<Inner>) -> Self {
        Self::filter_map(outer, |outer| Some(borrow_fn(outer))).unwrap()
    }

    #[inline]
    pub fn filter_map(
        outer: Ref<'a, Outer>,
        borrow_fn: impl FnOnce(&Outer) -> Option<Ref<Inner>>,
    ) -> Option<Self> {
        let boxed_outer = Box::new(outer);
        let inner: Ref<Inner> = borrow_fn(&boxed_outer)?;
        // SAFETY: The lifetime of the `inner` returned from `borrow_fn` is the
        // (anonymous) lifetime of `boxed_outer`. However we only provided the
        // closure with the reference to the *contents* of the box, `outer`, which
        // has lifetime 'a. This is safe as long as we ensure that the transmuted
        // `boxed_outer` outlives the transmuted `inner`.
        let inner: Ref<'a, Inner> = unsafe { std::mem::transmute(inner) };
        Some(Self {
            inner,
            _outer: boxed_outer,
        })
    }
}
impl<Inner, Outer> std::ops::Deref for NestedRef<'_, Inner, Outer>
where
    Inner: 'static,
{
    type Target = Inner;

    fn deref(&self) -> &Self::Target {
        self.inner.deref()
    }
}

/// A nested [`std::cell::Ref`]. Useful for chaining a mutable borrow of a
/// [`std::cell::RefCell`].
pub struct NestedRefMut<'a, Inner, Outer>
where
    Inner: 'static,
{
    // unsafely points to the Ref inside `_outer`.
    // Must be declared first so that it's dropped first.
    inner: RefMut<'a, Inner>,
    // Boxed so that `Self` is movable without breaking `inner`'s reference.
    _outer: Box<Ref<'a, Outer>>,
}
impl<'a, Inner, Outer> NestedRefMut<'a, Inner, Outer>
where
    Inner: 'static,
{
    #[inline]
    pub fn map(outer: Ref<'a, Outer>, borrow_fn: impl FnOnce(&Outer) -> RefMut<Inner>) -> Self {
        Self::filter_map(outer, |outer| Some(borrow_fn(outer))).unwrap()
    }

    #[inline]
    pub fn filter_map(
        outer: Ref<'a, Outer>,
        borrow_fn: impl FnOnce(&Outer) -> Option<RefMut<Inner>>,
    ) -> Option<Self> {
        let outer = Box::new(outer);
        let inner: RefMut<Inner> = borrow_fn(&outer)?;
        // SAFETY: The lifetime of the `inner` returned from `borrow_fn` is the
        // (anonymous) lifetime of `boxed_outer`. However we only provided the
        // closure with the reference to the *contents* of the box, `outer`, which
        // has lifetime 'a. This is safe as long as we ensure that the transmuted
        // `boxed_outer` outlives the transmuted `inner`.
        let inner: RefMut<'a, Inner> = unsafe { std::mem::transmute(inner) };
        Some(Self {
            inner,
            _outer: outer,
        })
    }
}
impl<Inner, Outer> std::ops::Deref for NestedRefMut<'_, Inner, Outer>
where
    Inner: 'static,
{
    type Target = Inner;

    fn deref(&self) -> &Self::Target {
        self.inner.deref()
    }
}
impl<Inner, Outer> std::ops::DerefMut for NestedRefMut<'_, Inner, Outer>
where
    Inner: 'static,
{
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.inner.deref_mut()
    }
}

#[cfg(test)]
mod tests {
    use std::cell::RefCell;

    use super::*;

    struct TestOuter {
        x: RefCell<i32>,
    }

    #[test]
    fn nestedref_map() {
        let outer = RefCell::new(TestOuter {
            x: RefCell::new(42),
        });
        let nested = NestedRef::map(outer.borrow(), |inner| inner.x.borrow());
        assert_eq!(*nested, 42);
    }

    #[test]
    fn nestedref_filter_map_some() {
        let outer = RefCell::new(TestOuter {
            x: RefCell::new(42),
        });
        let nested = NestedRef::filter_map(outer.borrow(), |inner| Some(inner.x.borrow()));
        let nested = nested.unwrap();
        assert_eq!(*nested, 42);
    }

    #[test]
    fn nestedref_filter_map_none() {
        let outer = RefCell::new(TestOuter {
            x: RefCell::new(42),
        });
        let nested = NestedRef::<i32, TestOuter>::filter_map(outer.borrow(), |_inner| None);
        assert!(nested.is_none());
    }

    #[test]
    fn nestedref_is_movable() {
        let outer = RefCell::new(TestOuter {
            x: RefCell::new(42),
        });
        let nested = NestedRef::map(outer.borrow(), |inner| inner.x.borrow());
        assert_eq!(*nested, 42);
        let boxed_nested = Box::new(nested);
        assert_eq!(**boxed_nested, 42);
    }

    #[test]
    fn nestedrefmut_map() {
        let outer = RefCell::new(TestOuter {
            x: RefCell::new(42),
        });
        {
            let mut nested = NestedRefMut::map(outer.borrow(), |inner| inner.x.borrow_mut());
            assert_eq!(*nested, 42);
            *nested += 1;
            assert_eq!(*nested, 43);
            assert!(outer.borrow().x.try_borrow().is_err());
        }
        assert_eq!(*outer.borrow().x.borrow(), 43);
    }

    #[test]
    fn nestedrefmut_filter_map_some() {
        let outer = RefCell::new(TestOuter {
            x: RefCell::new(42),
        });
        {
            let nested =
                NestedRefMut::filter_map(outer.borrow(), |inner| Some(inner.x.borrow_mut()));
            let mut nested = nested.unwrap();
            assert_eq!(*nested, 42);
            *nested += 1;
            assert_eq!(*nested, 43);
            assert!(outer.borrow().x.try_borrow().is_err());
        }
        assert_eq!(*outer.borrow().x.borrow(), 43);
    }

    #[test]
    fn nestedrefmut_filter_map_none() {
        let outer = RefCell::new(TestOuter {
            x: RefCell::new(42),
        });
        let nested = NestedRefMut::<i32, TestOuter>::filter_map(outer.borrow(), |_inner| None);
        assert!(nested.is_none());
    }

    #[test]
    fn nestedrefmut_is_movable() {
        let outer = RefCell::new(TestOuter {
            x: RefCell::new(42),
        });
        {
            let nested = NestedRefMut::map(outer.borrow(), |inner| inner.x.borrow_mut());
            assert_eq!(*nested, 42);
            let mut nested = Box::new(nested);
            assert_eq!(**nested, 42);
            **nested += 1;
            assert_eq!(**nested, 43);
        }
        assert_eq!(*outer.borrow().x.borrow(), 43);
    }
}