Debian Codegraph @

src/lib.rs

raw symbols SCIP
1 //! This crate provides several functions for handling `&mut T` including `take()`.
2 //!
3 //! `take()` allows for taking `T` out of a `&mut T`, doing anything with it including consuming it, and producing another `T` to put back in the `&mut T`.
4 //!
5 //! During `take()`, if a panic occurs, the entire process will be aborted, as there's no valid `T` to put back into the `&mut T`.
6 //! Use `take_or_recover()` to replace the `&mut T` with a recovery value before continuing the panic.
7 //!
8 //! Contrast with `std::mem::replace()`, which allows for putting a different `T` into a `&mut T`, but requiring the new `T` to be available before being able to consume the old `T`.
9
10 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/;
11
12 pub mod scopedrust-analyzer cargo take_mut 0.2.2 scoped/;
13
14 /// Allows use of a value pointed to by `&mut T` as though it was owned, as long as a `T` is made available afterwards.
15 ///
16 /// The closure must return a valid T.
17 /// # Important
18 /// Will abort the program if the closure panics.
19 ///
20 /// # Example
21 /// ```
22 /// struct Foo;
23 /// let mut foo = Foo;
24 /// take_mut::take(&mut foo, |foo| {
25 /// // Can now consume the Foo, and provide a new value later
26 /// drop(foo);
27 /// // Do more stuff
28 /// Foo // Return new Foo from closure, which goes back into the &mut Foo
29 /// });
30 /// ```
31 pub fn takerust-analyzer cargo take_mut 0.2.2 take().<T, F>(mut_ref: &mut T, closure: F)
32 where F: FnOncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnOnce#(T) -> T {
33 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/;
34
35 unsafe {
36 let old_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);
37 let new_t = 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#(|| closure(old_t)))
38 .unwrap_or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap_or_else().(|_| ::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().());
39 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().(mut_ref, new_t);
40 }
41 }
42
43 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
44 fn it_worksrust-analyzer cargo take_mut 0.2.2 it_works().() {
45 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)]
46 enum Foorust-analyzer cargo take_mut 0.2.2 Foo# {Arust-analyzer cargo take_mut 0.2.2 Foo#A#, Brust-analyzer cargo take_mut 0.2.2 Foo#B#};
47 impl Droprust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/drop/Drop# for Foorust-analyzer cargo take_mut 0.2.2 Foo# {
48 fn droprust-analyzer cargo take_mut 0.2.2 impl#[Foo][Drop]drop().(&mut self) {
49 match *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().self {
50 Foorust-analyzer cargo take_mut 0.2.2 Foo#::Arust-analyzer cargo take_mut 0.2.2 Foo#A# => printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("Foo::A dropped"),
51 Foorust-analyzer cargo take_mut 0.2.2 Foo#::Brust-analyzer cargo take_mut 0.2.2 Foo#B# => printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("Foo::B dropped")
52 }
53 }
54 }
55 let mut foo = Foorust-analyzer cargo take_mut 0.2.2 Foo#::Arust-analyzer cargo take_mut 0.2.2 Foo#A#;
56 takerust-analyzer cargo take_mut 0.2.2 take().(&mut foo, |f| {
57 droprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/drop().(f);
58 Foorust-analyzer cargo take_mut 0.2.2 Foo#::Brust-analyzer cargo take_mut 0.2.2 Foo#B#
59 });
60 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(&foo, &Foorust-analyzer cargo take_mut 0.2.2 Foo#::Brust-analyzer cargo take_mut 0.2.2 Foo#B#);
61 }
62
63
64 /// Allows use of a value pointed to by `&mut T` as though it was owned, as long as a `T` is made available afterwards.
65 ///
66 /// The closure must return a valid T.
67 /// # Important
68 /// Will replace `&mut T` with `recover` if the closure panics, then continues the panic.
69 ///
70 /// # Example
71 /// ```
72 /// struct Foo;
73 /// let mut foo = Foo;
74 /// take_mut::take_or_recover(&mut foo, || Foo, |foo| {
75 /// // Can now consume the Foo, and provide a new value later
76 /// drop(foo);
77 /// // Do more stuff
78 /// Foo // Return new Foo from closure, which goes back into the &mut Foo
79 /// });
80 /// ```
81 pub fn take_or_recoverrust-analyzer cargo take_mut 0.2.2 take_or_recover().<T, F, R>(mut_ref: &mut T, recover: R, closure: F)
82 where F: FnOncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnOnce#(T) -> T, R: FnOncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnOnce#() -> T {
83 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/;
84 unsafe {
85 let old_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);
86 let new_t = 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#(|| closure(old_t)));
87 match new_t {
88 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(err) => {
89 let r = 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#(|| recover()))
90 .unwrap_or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap_or_else().(|_| ::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().());
91 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().(mut_ref, r);
92 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().(err);
93 }
94 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(new_t) => 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().(mut_ref, new_t),
95 }
96 }
97 }
98
99
100
101
102 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
103 fn it_works_recoverrust-analyzer cargo take_mut 0.2.2 it_works_recover().() {
104 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)]
105 enum Foorust-analyzer cargo take_mut 0.2.2 Foo# {Arust-analyzer cargo take_mut 0.2.2 Foo#A#, Brust-analyzer cargo take_mut 0.2.2 Foo#B#};
106 impl Droprust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/drop/Drop# for Foorust-analyzer cargo take_mut 0.2.2 Foo# {
107 fn droprust-analyzer cargo take_mut 0.2.2 impl#[Foo][Drop]drop().(&mut self) {
108 match *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().self {
109 Foorust-analyzer cargo take_mut 0.2.2 Foo#::Arust-analyzer cargo take_mut 0.2.2 Foo#A# => printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("Foo::A dropped"),
110 Foorust-analyzer cargo take_mut 0.2.2 Foo#::Brust-analyzer cargo take_mut 0.2.2 Foo#B# => printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("Foo::B dropped")
111 }
112 }
113 }
114 let mut foo = Foorust-analyzer cargo take_mut 0.2.2 Foo#::Arust-analyzer cargo take_mut 0.2.2 Foo#A#;
115 take_or_recoverrust-analyzer cargo take_mut 0.2.2 take_or_recover().(&mut foo, || Foorust-analyzer cargo take_mut 0.2.2 Foo#::Arust-analyzer cargo take_mut 0.2.2 Foo#A#, |f| {
116 droprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/drop().(f);
117 Foorust-analyzer cargo take_mut 0.2.2 Foo#::Brust-analyzer cargo take_mut 0.2.2 Foo#B#
118 });
119 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(&foo, &Foorust-analyzer cargo take_mut 0.2.2 Foo#::Brust-analyzer cargo take_mut 0.2.2 Foo#B#);
120 }
121
122 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
123 fn it_works_recover_panicrust-analyzer cargo take_mut 0.2.2 it_works_recover_panic().() {
124 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)]
125 enum Foorust-analyzer cargo take_mut 0.2.2 Foo# {Arust-analyzer cargo take_mut 0.2.2 Foo#A#, Brust-analyzer cargo take_mut 0.2.2 Foo#B#, Crust-analyzer cargo take_mut 0.2.2 Foo#C#};
126 impl Droprust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/drop/Drop# for Foorust-analyzer cargo take_mut 0.2.2 Foo# {
127 fn droprust-analyzer cargo take_mut 0.2.2 impl#[Foo][Drop]drop().(&mut self) {
128 match *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().self {
129 Foorust-analyzer cargo take_mut 0.2.2 Foo#::Arust-analyzer cargo take_mut 0.2.2 Foo#A# => printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("Foo::A dropped"),
130 Foorust-analyzer cargo take_mut 0.2.2 Foo#::Brust-analyzer cargo take_mut 0.2.2 Foo#B# => printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("Foo::B dropped"),
131 Foorust-analyzer cargo take_mut 0.2.2 Foo#::Crust-analyzer cargo take_mut 0.2.2 Foo#C# => printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("Foo::C dropped")
132 }
133 }
134 }
135 let mut foo = Foorust-analyzer cargo take_mut 0.2.2 Foo#::Arust-analyzer cargo take_mut 0.2.2 Foo#A#;
136
137 let res = 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#(|| {
138 take_or_recoverrust-analyzer cargo take_mut 0.2.2 take_or_recover().(&mut foo, || Foorust-analyzer cargo take_mut 0.2.2 Foo#::Crust-analyzer cargo take_mut 0.2.2 Foo#C#, |f| {
139 droprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/drop().(f);
140 panicrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/panic!!("panic");
141 Foorust-analyzer cargo take_mut 0.2.2 Foo#::Brust-analyzer cargo take_mut 0.2.2 Foo#B#
142 });
143 }));
144
145 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(res.is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().());
146 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(&foo, &Foorust-analyzer cargo take_mut 0.2.2 Foo#::Crust-analyzer cargo take_mut 0.2.2 Foo#C#);
147 }
148
149
150