| 1 | //! Cell allowing the inner value to be consumed without a mutable reference. |
| 2 | //! |
| 3 | //! In order to maintain safety, it is not possible to get a reference to the |
| 4 | //! inner value. |
| 5 | |
| 6 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/; |
| 7 | 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/::UnsafeCellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/UnsafeCell#; |
| 8 | |
| 9 | /// Cell allowing the inner value to be consumed without a mutable reference. |
| 10 | pub struct Takerust-analyzer cargo take 0.1.0 Take#<T> { |
| 11 | valrust-analyzer cargo take 0.1.0 Take#val.: UnsafeCellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/UnsafeCell#<Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<T>>, |
| 12 | } |
| 13 | |
| 14 | impl<T> Takerust-analyzer cargo take 0.1.0 Take#<T> { |
| 15 | /// Create and return a new `Take` value containing the given inner value. |
| 16 | pub fn newrust-analyzer cargo take 0.1.0 impl#[`Take<T>`]new().(val: T) -> Takerust-analyzer cargo take 0.1.0 Take#<T> { |
| 17 | Takerust-analyzer cargo take 0.1.0 Take# { valrust-analyzer cargo take 0.1.0 Take#val.: UnsafeCellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/UnsafeCell#::newrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`UnsafeCell<T>`]new().(Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(val)) } |
| 18 | } |
| 19 | |
| 20 | /// Consume and return the inner value. |
| 21 | /// |
| 22 | /// # Panics |
| 23 | /// |
| 24 | /// If the inner value has already been consumed, the call will panic. |
| 25 | pub fn takerust-analyzer cargo take 0.1.0 impl#[`Take<T>`]take().(&self) -> T { |
| 26 | unsafe { (*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref#deref().self.valrust-analyzer cargo take 0.1.0 Take#val..getrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`UnsafeCell<T>`]get().()).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().("value already consumed") |
| 27 | } |
| 28 | |
| 29 | // This function cannot be public as no public API functions should be able |
| 30 | // to return a reference to the inner value. It is only safe to grab this |
| 31 | // reference if `take` is not called while the reference is held. |
| 32 | fn innerrust-analyzer cargo take 0.1.0 impl#[`Take<T>`]inner().(&self) -> &Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<T> { |
| 33 | unsafe { &*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref#deref().self.valrust-analyzer cargo take 0.1.0 Take#val..getrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`UnsafeCell<T>`]get().() } |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | impl<T: fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug#> fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug# for Takerust-analyzer cargo take 0.1.0 Take#<T> { |
| 38 | fn fmtrust-analyzer cargo take 0.1.0 impl#[`Take<T>`][Debug]fmt().(&self, fmt: &mut fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#) -> fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# { |
| 39 | fmt.debug_structrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/impl#[`Formatter<'a>`]debug_struct().("Take") |
| 40 | .fieldrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/builders/impl#[`DebugStruct<'a, 'b>`]field().("val", self.innerrust-analyzer cargo take 0.1.0 impl#[`Take<T>`]inner().()) |
| 41 | .finishrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/builders/impl#[`DebugStruct<'a, 'b>`]finish().() |
| 42 | } |
| 43 | } |