| 1 | //! This module provides a scoped API, allowing for taking an arbitrary number of `&mut T` into `T` within one closure. |
| 2 | //! The references are all required to outlive the closure. |
| 3 | //! |
| 4 | //! # Example |
| 5 | //! ``` |
| 6 | //! use take_mut::scoped; |
| 7 | //! struct Foo; |
| 8 | //! let mut foo = Foo; // Must outlive scope |
| 9 | //! scoped::scope(|scope| { |
| 10 | //! let (t, hole) = scope.take(&mut foo); |
| 11 | //! drop(t); |
| 12 | //! hole.fill(Foo); // If not called before the closure ends, causes an abort. |
| 13 | //! }); |
| 14 | //! ``` |
| 15 | //! |
| 16 | //! # Invalid Example (does not compile) |
| 17 | //! ```ignore |
| 18 | //! use take_mut::scoped; |
| 19 | //! struct Foo; |
| 20 | //! scoped::scope(|scope| { |
| 21 | //! let mut foo = Foo; // Invalid because foo must come from outside the scope. |
| 22 | //! let (t, hole) = scope.take(&mut foo); |
| 23 | //! drop(t); |
| 24 | //! hole.fill(Foo); |
| 25 | //! }); |
| 26 | //! ``` |
| 27 | //! |
| 28 | //! `Scope` also offers `take_or_recover`, which takes a function to call in the event the hole isn't filled. |
| 29 | |
| 30 | #![warn(missing_docs)] |
| 31 | |
| 32 | |
| 33 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/; |
| 34 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::panicrust-analyzer cargo std https://github.com/rust-lang/rust/library/std panic/; |
| 35 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::cellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/::Cellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/Cell#; |
| 36 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::markerrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/::PhantomDatarust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/PhantomData#; |
| 37 | |
| 38 | /// Represents a scope within which, it is possible to take a `T` from a `&mut T` as long as the `&mut T` outlives the scope. |
| 39 | pub struct Scoperust-analyzer cargo take_mut 0.2.2 scoped/Scope#<'s> { |
| 40 | active_holesrust-analyzer cargo take_mut 0.2.2 scoped/Scope#active_holes.: Cellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/Cell#<usize>, |
| 41 | markerrust-analyzer cargo take_mut 0.2.2 scoped/Scope#marker.: PhantomDatarust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/PhantomData#<Cellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/Cell#<&'s mut ()>> |
| 42 | } |
| 43 | |
| 44 | impl<'s> Scoperust-analyzer cargo take_mut 0.2.2 scoped/Scope#<'s> { |
| 45 | |
| 46 | /// Takes a `(T, Hole<'c, 'm, T, F>)` from an `&'m mut T`. |
| 47 | /// |
| 48 | /// If the `Hole` is dropped without being filled, either due to panic or forgetting to fill, will run the `recovery` function to obtain a `T` to fill itself with. |
| 49 | pub fn take_or_recoverrust-analyzer cargo take_mut 0.2.2 scoped/impl#[`Scope<'s>`]take_or_recover().<'c, 'm: 's, T: 'm, F: FnOncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnOnce#() -> T>(&'c self, mut_ref: &'m mut T, recovery: F) -> (T, Holerust-analyzer cargo take_mut 0.2.2 scoped/Hole#<'c, 'm, T, F>) { |
| 50 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/; |
| 51 | |
| 52 | let t: T; |
| 53 | let hole: Holerust-analyzer cargo take_mut 0.2.2 scoped/Hole#<'c, 'm, T, F>; |
| 54 | let num_of_holes = self.active_holesrust-analyzer cargo take_mut 0.2.2 scoped/Scope#active_holes..getrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`Cell<T>`]get().(); |
| 55 | if num_of_holes rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::usizerust-analyzer cargo core https://github.com/rust-lang/rust/library/core legacy_int_modules/usize/::MAXrust-analyzer cargo core https://github.com/rust-lang/rust/library/core legacy_int_modules/usize/MAX. { |
| 56 | panicrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/panic!!("Too many holes!"); |
| 57 | } |
| 58 | self.active_holesrust-analyzer cargo take_mut 0.2.2 scoped/Scope#active_holes..setrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`Cell<T>`]set().(num_of_holes rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1); |
| 59 | unsafe { |
| 60 | t = ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/::readrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/read().(mut_ref as *mut T); |
| 61 | hole = Holerust-analyzer cargo take_mut 0.2.2 scoped/Hole# { |
| 62 | active_holesrust-analyzer cargo take_mut 0.2.2 scoped/Hole#active_holes.: &self.active_holesrust-analyzer cargo take_mut 0.2.2 scoped/Scope#active_holes., |
| 63 | holerust-analyzer cargo take_mut 0.2.2 scoped/Hole#hole.: mut_ref as *mut T, |
| 64 | phantomrust-analyzer cargo take_mut 0.2.2 scoped/Hole#phantom.: PhantomDatarust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/PhantomData#, |
| 65 | recoveryrust-analyzer cargo take_mut 0.2.2 scoped/Hole#recovery.: Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(recovery) |
| 66 | }; |
| 67 | }; |
| 68 | (t, hole) |
| 69 | } |
| 70 | |
| 71 | /// Takes a `(T, Hole<'c, 'm, T, F>)` from an `&'m mut T`. |
| 72 | pub fn takerust-analyzer cargo take_mut 0.2.2 scoped/impl#[`Scope<'s>`]take().<'c, 'm: 's, T: 'm>(&'c self, mut_ref: &'m mut T) -> (T, Holerust-analyzer cargo take_mut 0.2.2 scoped/Hole#<'c, 'm, T, fn() -> T>) { |
| 73 | #[allow(missing_docs)] |
| 74 | fn panicrust-analyzer cargo take_mut 0.2.2 scoped/panic().<T>() -> T { |
| 75 | panicrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/panic!!("Failed to recover a Hole!") |
| 76 | } |
| 77 | self.take_or_recoverrust-analyzer cargo take_mut 0.2.2 scoped/impl#[`Scope<'s>`]take_or_recover().(mut_ref, panicrust-analyzer cargo take_mut 0.2.2 scoped/panic().) |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | /// Main function to create a `Scope`. |
| 82 | /// |
| 83 | /// If the given closure ends without all Holes filled, will abort the program. |
| 84 | pub fn scoperust-analyzer cargo take_mut 0.2.2 scoped/scope().<'s, F, R>(f: F) -> R |
| 85 | where F: FnOncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnOnce#(&Scoperust-analyzer cargo take_mut 0.2.2 scoped/Scope#<'s>) -> R { |
| 86 | let this = Scoperust-analyzer cargo take_mut 0.2.2 scoped/Scope# { active_holesrust-analyzer cargo take_mut 0.2.2 scoped/Scope#active_holes.: Cellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/Cell#::newrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`Cell<T>`]new().(0), markerrust-analyzer cargo take_mut 0.2.2 scoped/Scope#marker.: PhantomDatarust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/PhantomData# }; |
| 87 | let result = panicrust-analyzer cargo std https://github.com/rust-lang/rust/library/std panic/::catch_unwindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std panic/catch_unwind().(panicrust-analyzer cargo std https://github.com/rust-lang/rust/library/std panic/::AssertUnwindSaferust-analyzer cargo core https://github.com/rust-lang/rust/library/core panic/unwind_safe/AssertUnwindSafe#(|| { |
| 88 | f(&this) |
| 89 | })); |
| 90 | if this.active_holesrust-analyzer cargo take_mut 0.2.2 scoped/Scope#active_holes..getrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`Cell<T>`]get().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne().0 { |
| 91 | stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::processrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/::abortrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/abort().(); |
| 92 | } |
| 93 | match result { |
| 94 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(r) => r, |
| 95 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(p) => panicrust-analyzer cargo std https://github.com/rust-lang/rust/library/std panic/::resume_unwindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std panic/resume_unwind().(p), |
| 96 | } |
| 97 | |
| 98 | } |
| 99 | |
| 100 | /// A `Hole<'c, 'm, T, F>` represents an unfilled `&'m mut T` which must be filled before the end of the `Scope` with lifetime `'c` and recovery closure `F`. |
| 101 | /// |
| 102 | /// An unfilled `Hole<'c, 'm, T, F> that is destructed will try to use `F` to fill the hole. |
| 103 | /// |
| 104 | /// If the scope ends without the `Hole` being filled, the program will `std::process::abort()`. |
| 105 | #[must_use] |
| 106 | pub struct Holerust-analyzer cargo take_mut 0.2.2 scoped/Hole#<'c, 'm, T: 'm, F: FnOncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnOnce#() -> T> { |
| 107 | active_holesrust-analyzer cargo take_mut 0.2.2 scoped/Hole#active_holes.: &'c Cellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/Cell#<usize>, |
| 108 | holerust-analyzer cargo take_mut 0.2.2 scoped/Hole#hole.: *mut T, |
| 109 | phantomrust-analyzer cargo take_mut 0.2.2 scoped/Hole#phantom.: PhantomDatarust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/PhantomData#<&'m mut T>, |
| 110 | recoveryrust-analyzer cargo take_mut 0.2.2 scoped/Hole#recovery.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<F>, |
| 111 | } |
| 112 | |
| 113 | impl<'c, 'm, T: 'm, F: FnOncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnOnce#() -> T> Holerust-analyzer cargo take_mut 0.2.2 scoped/Hole#<'c, 'm, T, F> { |
| 114 | /// Fills the Hole. |
| 115 | pub fn fillrust-analyzer cargo take_mut 0.2.2 scoped/impl#[`Hole<'c, 'm, T, F>`]fill().(self, t: T) { |
| 116 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/; |
| 117 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/; |
| 118 | |
| 119 | unsafe { |
| 120 | ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/::writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/write().(self.holerust-analyzer cargo take_mut 0.2.2 scoped/Hole#hole., t); |
| 121 | } |
| 122 | let num_holes = self.active_holesrust-analyzer cargo take_mut 0.2.2 scoped/Hole#active_holes..getrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`Cell<T>`]get().(); |
| 123 | self.active_holesrust-analyzer cargo take_mut 0.2.2 scoped/Hole#active_holes..setrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`Cell<T>`]set().(num_holes rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().1); |
| 124 | memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::forgetrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/forget().(self); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | impl<'c, 'm, T: 'm, F: FnOncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnOnce#() -> T> Droprust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/drop/Drop# for Holerust-analyzer cargo take_mut 0.2.2 scoped/Hole#<'c, 'm, T, F> { |
| 129 | fn droprust-analyzer cargo take_mut 0.2.2 scoped/impl#[`Hole<'c, 'm, T, F>`][Drop]drop().(&mut self) { |
| 130 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/; |
| 131 | |
| 132 | let t = (self.recoveryrust-analyzer cargo take_mut 0.2.2 scoped/Hole#recovery..takerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]take().().expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]expect().("No recovery function in Hole!"))(); |
| 133 | unsafe { |
| 134 | ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/::writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/write().(self.holerust-analyzer cargo take_mut 0.2.2 scoped/Hole#hole., t); |
| 135 | } |
| 136 | let num_holes = self.active_holesrust-analyzer cargo take_mut 0.2.2 scoped/Hole#active_holes..getrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`Cell<T>`]get().(); |
| 137 | self.active_holesrust-analyzer cargo take_mut 0.2.2 scoped/Hole#active_holes..setrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`Cell<T>`]set().(num_holes rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().1); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 142 | fn scope_based_takerust-analyzer cargo take_mut 0.2.2 scoped/scope_based_take().() { |
| 143 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 144 | struct Foorust-analyzer cargo take_mut 0.2.2 scoped/Foo#; |
| 145 | |
| 146 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 147 | struct Barrust-analyzer cargo take_mut 0.2.2 scoped/Bar# { |
| 148 | arust-analyzer cargo take_mut 0.2.2 scoped/Bar#a.: Foorust-analyzer cargo take_mut 0.2.2 scoped/Foo#, |
| 149 | brust-analyzer cargo take_mut 0.2.2 scoped/Bar#b.: Foorust-analyzer cargo take_mut 0.2.2 scoped/Foo# |
| 150 | } |
| 151 | let mut bar = Barrust-analyzer cargo take_mut 0.2.2 scoped/Bar# { arust-analyzer cargo take_mut 0.2.2 scoped/Bar#a.: Foorust-analyzer cargo take_mut 0.2.2 scoped/Foo#, brust-analyzer cargo take_mut 0.2.2 scoped/Bar#b.: Foorust-analyzer cargo take_mut 0.2.2 scoped/Foo# }; |
| 152 | scoperust-analyzer cargo take_mut 0.2.2 scoped/scope().(|scope| { |
| 153 | let (a, a_hole) = scope.takerust-analyzer cargo take_mut 0.2.2 scoped/impl#[`Scope<'s>`]take().(&mut bar.arust-analyzer cargo take_mut 0.2.2 scoped/Bar#a.); |
| 154 | let (b, b_hole) = scope.takerust-analyzer cargo take_mut 0.2.2 scoped/impl#[`Scope<'s>`]take().(&mut bar.brust-analyzer cargo take_mut 0.2.2 scoped/Bar#b.); |
| 155 | // Imagine consuming a and b |
| 156 | a_hole.fillrust-analyzer cargo take_mut 0.2.2 scoped/impl#[`Hole<'c, 'm, T, F>`]fill().(Foorust-analyzer cargo take_mut 0.2.2 scoped/Foo#); |
| 157 | b_hole.fillrust-analyzer cargo take_mut 0.2.2 scoped/impl#[`Hole<'c, 'm, T, F>`]fill().(Foorust-analyzer cargo take_mut 0.2.2 scoped/Foo#); |
| 158 | }); |
| 159 | printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("{:?}", &bar); |
| 160 | } |
| 161 | |
| 162 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 163 | fn panic_on_recovered_panicrust-analyzer cargo take_mut 0.2.2 scoped/panic_on_recovered_panic().() { |
| 164 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::panicrust-analyzer cargo std https://github.com/rust-lang/rust/library/std panic/; |
| 165 | |
| 166 | struct Foorust-analyzer cargo take_mut 0.2.2 scoped/Foo#; |
| 167 | let mut foo = Foorust-analyzer cargo take_mut 0.2.2 scoped/Foo#; |
| 168 | let result = panicrust-analyzer cargo std https://github.com/rust-lang/rust/library/std panic/::catch_unwindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std panic/catch_unwind().(panicrust-analyzer cargo std https://github.com/rust-lang/rust/library/std panic/::AssertUnwindSaferust-analyzer cargo core https://github.com/rust-lang/rust/library/core panic/unwind_safe/AssertUnwindSafe#(|| { |
| 169 | scoperust-analyzer cargo take_mut 0.2.2 scoped/scope().(|scope| { |
| 170 | let (t, hole) = scope.take_or_recoverrust-analyzer cargo take_mut 0.2.2 scoped/impl#[`Scope<'s>`]take_or_recover().(&mut foo, || Foorust-analyzer cargo take_mut 0.2.2 scoped/Foo#); |
| 171 | panicrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/panic!!("Oops!"); |
| 172 | }); |
| 173 | })); |
| 174 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(result.is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().()); |
| 175 | } |