| 1 | /*! |
| 2 | A utility type that allows you to defer dropping your data to a background |
| 3 | thread. See [`DeferDrop`] for details. |
| 4 | |
| 5 | Inspired by [https://abramov.io/rust-dropping-things-in-another-thread](https://abramov.io/rust-dropping-things-in-another-thread) |
| 6 | |
| 7 | # Features |
| 8 | |
| 9 | - `serde`: when enabled, adds a [`Serialize`] and [`Deserialize`] |
| 10 | implementation to [`DeferDrop`] |
| 11 | */ |
| 12 | |
| 13 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::{ |
| 14 | memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::{selfrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/, ManuallyDroprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/manually_drop/ManuallyDrop#}, |
| 15 | opsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/::{Derefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref#, DerefMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/DerefMut#}, |
| 16 | threadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/::{selfrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/, JoinHandlerust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/join_handle/JoinHandle#}, |
| 17 | }; |
| 18 | |
| 19 | use crossbeam_channelrust-analyzer cargo crossbeam-channel 0.5.6 crate/::{selfrust-analyzer cargo crossbeam-channel 0.5.6 crate/ as channelrust-analyzer cargo crossbeam-channel 0.5.6 crate/, Senderrust-analyzer cargo crossbeam-channel 0.5.6 channel/Sender#}; |
| 20 | use once_cellrust-analyzer cargo once_cell 1.16.0 crate/::syncrust-analyzer cargo once_cell 1.16.0 sync/::OnceCellrust-analyzer cargo once_cell 1.16.0 sync/OnceCell#; |
| 21 | |
| 22 | #[cfg(feature = "serde")] |
| 23 | use serderust-analyzer cargo serde 1.0.147 crate/::{Deserializerust-analyzer cargo serde 1.0.147 de/Deserialize#, Serializerust-analyzer cargo serde 1.0.147 ser/Serialize#}; |
| 24 | |
| 25 | /// Wrapper type that, when dropped, sends the inner value to a global |
| 26 | /// background thread to be dropped. Useful in cases where a value takes a |
| 27 | /// long time to drop (for instance, a windows file that might block on close, |
| 28 | /// or a large data structure that has to extensively recursively trawl |
| 29 | /// itself). |
| 30 | /// |
| 31 | /// `DeferDrop` implements `Deref` and `DerefMut`, meaning it can be |
| 32 | /// dereferenced and freely used like a container around its inner type. |
| 33 | /// |
| 34 | /// # Notes: |
| 35 | /// |
| 36 | /// Carefully consider whether this pattern is necessary for your use case. |
| 37 | /// Like all worker-thread abstractions, sending the value to a separate |
| 38 | /// thread comes with its own costs, so it should only be done if performance |
| 39 | /// profiling indicates that it's a performance gain. |
| 40 | /// |
| 41 | /// There is only one global worker thread. Dropped values are enqueued in an |
| 42 | /// unbounded channel to be consumed by this thread; if you produce more |
| 43 | /// garbage than the thread can handle, this will cause unbounded memory |
| 44 | /// consumption. There is currently no way for the thread to signal or block |
| 45 | /// if it is overwhelmed. |
| 46 | /// |
| 47 | /// All of the standard non-determinism threading caveats apply here. The |
| 48 | /// objects are guaranteed to be destructed in the order received through a |
| 49 | /// channel, which means that objects sent from a single thread will be |
| 50 | /// destructed in order. However, there is no guarantee about the ordering of |
| 51 | /// interleaved values from different threads. Additionally, there are no |
| 52 | /// guarantees about how long the values will be queued before being dropped, |
| 53 | /// or even that they will be dropped at all. If your `main` thread terminates |
| 54 | /// before all drops could be completed, they will be silently lost (as though |
| 55 | /// via a [`mem::forget`]). This behavior is entirely up to your OS's thread |
| 56 | /// scheduler. There is no way to receive a signal indicating when a particular |
| 57 | /// object was dropped. |
| 58 | /// |
| 59 | /// # Example |
| 60 | /// |
| 61 | /// ``` |
| 62 | /// use defer_drop::DeferDrop; |
| 63 | /// use std::time::{Instant, Duration}; |
| 64 | /// use std::iter::repeat_with; |
| 65 | /// |
| 66 | /// let massive_vec: Vec<Vec<i32>> = repeat_with(|| vec![1, 2, 3]) |
| 67 | /// .take(1_000_000) |
| 68 | /// .collect(); |
| 69 | /// |
| 70 | /// let deferred = DeferDrop::new(massive_vec.clone()); |
| 71 | /// |
| 72 | /// fn timer(f: impl FnOnce()) -> Duration { |
| 73 | /// let start = Instant::now(); |
| 74 | /// f(); |
| 75 | /// Instant::now() - start |
| 76 | /// } |
| 77 | /// |
| 78 | /// let drop1 = timer(move || drop(massive_vec)); |
| 79 | /// let drop2 = timer(move || drop(deferred)); |
| 80 | /// |
| 81 | /// assert!(drop2 < drop1); |
| 82 | /// ``` |
| 83 | #[repr(transparent)] |
| 84 | #[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:, Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, 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:, Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/macros/Hash:, PartialOrdrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd:, Ordrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord:)] |
| 85 | pub struct DeferDroprust-analyzer cargo defer-drop 1.3.0 DeferDrop#<T: Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send# + 'static> { |
| 86 | innerrust-analyzer cargo defer-drop 1.3.0 DeferDrop#inner.: ManuallyDroprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/manually_drop/ManuallyDrop#<T>, |
| 87 | } |
| 88 | |
| 89 | impl<T: Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send# + 'static> DeferDroprust-analyzer cargo defer-drop 1.3.0 DeferDrop#<T> { |
| 90 | /// Create a new `DeferDrop` value. |
| 91 | #[inline] |
| 92 | pub fn newrust-analyzer cargo defer-drop 1.3.0 impl#[`DeferDrop<T>`]new().(value: T) -> Selfrust-analyzer cargo defer-drop 1.3.0 impl#[`DeferDrop<T>`] { |
| 93 | DeferDroprust-analyzer cargo defer-drop 1.3.0 DeferDrop# { |
| 94 | innerrust-analyzer cargo defer-drop 1.3.0 DeferDrop#inner.: ManuallyDroprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/manually_drop/ManuallyDrop#::newrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/manually_drop/impl#[`ManuallyDrop<T>`]new().(value), |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | /// Unwrap the `DeferDrop`, returning the inner value. This has the effect |
| 99 | /// of cancelling the deferred drop behavior; ownership of the inner value |
| 100 | /// is transferred to the caller. |
| 101 | pub fn into_innerrust-analyzer cargo defer-drop 1.3.0 impl#[`DeferDrop<T>`]into_inner().(mut this: Selfrust-analyzer cargo defer-drop 1.3.0 impl#[`DeferDrop<T>`]) -> T { |
| 102 | let value = unsafe { ManuallyDroprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/manually_drop/ManuallyDrop#::takerust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/manually_drop/impl#[`ManuallyDrop<T>`]take().(&mut this.innerrust-analyzer cargo defer-drop 1.3.0 DeferDrop#inner.) }; |
| 103 | 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().(this); |
| 104 | value |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | static GARBAGE_CANrust-analyzer cargo defer-drop 1.3.0 GARBAGE_CAN.: OnceCellrust-analyzer cargo once_cell 1.16.0 sync/OnceCell#<GarbageCanrust-analyzer cargo defer-drop 1.3.0 GarbageCan#> = OnceCellrust-analyzer cargo once_cell 1.16.0 sync/OnceCell#::newrust-analyzer cargo once_cell 1.16.0 sync/impl#[`OnceCell<T>`]new().(); |
| 109 | |
| 110 | impl<T: Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send# + 'static> Droprust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/drop/Drop# for DeferDroprust-analyzer cargo defer-drop 1.3.0 DeferDrop#<T> { |
| 111 | fn droprust-analyzer cargo defer-drop 1.3.0 impl#[`DeferDrop<T>`][Drop]drop().(&mut self) { |
| 112 | GARBAGE_CANrust-analyzer cargo defer-drop 1.3.0 GARBAGE_CAN. |
| 113 | .get_or_initrust-analyzer cargo once_cell 1.16.0 sync/impl#[`OnceCell<T>`]get_or_init().(|| GarbageCanrust-analyzer cargo defer-drop 1.3.0 GarbageCan#::newrust-analyzer cargo defer-drop 1.3.0 impl#[GarbageCan]new().("defer-drop background thread".to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().())) |
| 114 | .throw_awayrust-analyzer cargo defer-drop 1.3.0 impl#[GarbageCan]throw_away().(unsafe { ManuallyDroprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/manually_drop/ManuallyDrop#::takerust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/manually_drop/impl#[`ManuallyDrop<T>`]take().(&mut self.innerrust-analyzer cargo defer-drop 1.3.0 DeferDrop#inner.) }); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | impl<T: Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send# + 'static> Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<T> for DeferDroprust-analyzer cargo defer-drop 1.3.0 DeferDrop#<T> { |
| 119 | #[inline] |
| 120 | fn fromrust-analyzer cargo defer-drop 1.3.0 impl#[`DeferDrop<T>`][`From<T>`]from().(value: T) -> Selfrust-analyzer cargo defer-drop 1.3.0 impl#[`DeferDrop<T>`][`From<T>`] { |
| 121 | Selfrust-analyzer cargo defer-drop 1.3.0 impl#[`DeferDrop<T>`][`From<T>`]::newrust-analyzer cargo defer-drop 1.3.0 impl#[`DeferDrop<T>`]new().(value) |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | impl<T: Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send# + 'static> AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<T> for DeferDroprust-analyzer cargo defer-drop 1.3.0 DeferDrop#<T> { |
| 126 | #[inline] |
| 127 | fn as_refrust-analyzer cargo defer-drop 1.3.0 impl#[`DeferDrop<T>`][`AsRef<T>`]as_ref().(&self) -> &T { |
| 128 | &self.innerrust-analyzer cargo defer-drop 1.3.0 DeferDrop#inner. |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | impl<T: Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send# + 'static> AsMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsMut#<T> for DeferDroprust-analyzer cargo defer-drop 1.3.0 DeferDrop#<T> { |
| 133 | #[inline] |
| 134 | fn as_mutrust-analyzer cargo defer-drop 1.3.0 impl#[`DeferDrop<T>`][`AsMut<T>`]as_mut().(&mut self) -> &mut T { |
| 135 | &mut self.innerrust-analyzer cargo defer-drop 1.3.0 DeferDrop#inner. |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | impl<T: Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send# + 'static> Derefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref# for DeferDroprust-analyzer cargo defer-drop 1.3.0 DeferDrop#<T> { |
| 140 | type Targetrust-analyzer cargo defer-drop 1.3.0 impl#[`DeferDrop<T>`][Deref]Target# = T; |
| 141 | |
| 142 | #[inline] |
| 143 | fn derefrust-analyzer cargo defer-drop 1.3.0 impl#[`DeferDrop<T>`][Deref]deref().(&self) -> &Selfrust-analyzer cargo defer-drop 1.3.0 impl#[`DeferDrop<T>`][Deref]::Targetrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref#Target# { |
| 144 | &self.innerrust-analyzer cargo defer-drop 1.3.0 DeferDrop#inner. |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | impl<T: Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send# + 'static> DerefMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/DerefMut# for DeferDroprust-analyzer cargo defer-drop 1.3.0 DeferDrop#<T> { |
| 149 | #[inline] |
| 150 | fn deref_mutrust-analyzer cargo defer-drop 1.3.0 impl#[`DeferDrop<T>`][DerefMut]deref_mut().(&mut self) -> &mut Selfrust-analyzer cargo defer-drop 1.3.0 impl#[`DeferDrop<T>`][DerefMut]::Targetrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref#Target# { |
| 151 | &mut self.innerrust-analyzer cargo defer-drop 1.3.0 DeferDrop#inner. |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | #[cfg(feature = "serde")] |
| 156 | impl<T: Serializerust-analyzer cargo serde 1.0.147 ser/Serialize# + Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send# + 'static> Serializerust-analyzer cargo serde 1.0.147 ser/Serialize# for DeferDroprust-analyzer cargo defer-drop 1.3.0 DeferDrop#<T> { |
| 157 | fn serializerust-analyzer cargo defer-drop 1.3.0 impl#[`DeferDrop<T>`][Serialize]serialize().<S>(&self, serializer: S) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<S::Okrust-analyzer cargo serde 1.0.147 ser/Serializer#Ok#, S::Errorrust-analyzer cargo serde 1.0.147 ser/Serializer#Error#> |
| 158 | where |
| 159 | S: serderust-analyzer cargo serde 1.0.147 crate/::Serializerrust-analyzer cargo serde 1.0.147 ser/Serializer#, |
| 160 | { |
| 161 | self.as_refrust-analyzer cargo defer-drop 1.3.0 impl#[`DeferDrop<T>`][`AsRef<T>`]as_ref().().serializerust-analyzer cargo serde 1.0.147 ser/Serialize#serialize().(serializer) |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | #[cfg(feature = "serde")] |
| 166 | impl<'de, T: Deserializerust-analyzer cargo serde 1.0.147 de/Deserialize#<'de> + Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send# + 'static> Deserializerust-analyzer cargo serde 1.0.147 de/Deserialize#<'de> for DeferDroprust-analyzer cargo defer-drop 1.3.0 DeferDrop#<T> { |
| 167 | fn deserializerust-analyzer cargo defer-drop 1.3.0 impl#[`DeferDrop<T>`][`Deserialize<'de>`]deserialize().<D>(deserializer: D) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Selfrust-analyzer cargo defer-drop 1.3.0 impl#[`DeferDrop<T>`][`Deserialize<'de>`], D::Errorrust-analyzer cargo serde 1.0.147 de/Deserializer#Error#> |
| 168 | where |
| 169 | D: serderust-analyzer cargo serde 1.0.147 crate/::Deserializerrust-analyzer cargo serde 1.0.147 de/Deserializer#<'de>, |
| 170 | { |
| 171 | T::deserializerust-analyzer cargo serde 1.0.147 de/Deserialize#deserialize().(deserializer).maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map().(Selfrust-analyzer cargo defer-drop 1.3.0 impl#[`DeferDrop<T>`][`Deserialize<'de>`]::newrust-analyzer cargo defer-drop 1.3.0 impl#[`DeferDrop<T>`]new().) |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | struct GarbageCanrust-analyzer cargo defer-drop 1.3.0 GarbageCan# { |
| 176 | senderrust-analyzer cargo defer-drop 1.3.0 GarbageCan#sender.: Senderrust-analyzer cargo crossbeam-channel 0.5.6 channel/Sender#<Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#<dyn Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send#>>, |
| 177 | handlerust-analyzer cargo defer-drop 1.3.0 GarbageCan#handle.: JoinHandlerust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/join_handle/JoinHandle#<()>, |
| 178 | } |
| 179 | |
| 180 | impl GarbageCanrust-analyzer cargo defer-drop 1.3.0 GarbageCan# { |
| 181 | fn newrust-analyzer cargo defer-drop 1.3.0 impl#[GarbageCan]new().(name: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#) -> Selfrust-analyzer cargo defer-drop 1.3.0 impl#[GarbageCan] { |
| 182 | let (sender, receiver) = channelrust-analyzer cargo crossbeam-channel 0.5.6 crate/::unboundedrust-analyzer cargo crossbeam-channel 0.5.6 channel/unbounded().(); |
| 183 | |
| 184 | // TODO: drops should never panic, but if one does, we should |
| 185 | // probably abort the process |
| 186 | let handle = threadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/::Builderrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/builder/Builder#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/builder/impl#[Builder]new().() |
| 187 | .namerust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/builder/impl#[Builder]name().(name) |
| 188 | .spawnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/builder/impl#[Builder]spawn().(move || receiver.into_iterrust-analyzer cargo crossbeam-channel 0.5.6 channel/impl#[`Receiver<T>`][IntoIterator]into_iter().().for_eachrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#for_each().(droprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/drop().)) |
| 189 | .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("failed to spawn defer-drop background thread"); |
| 190 | |
| 191 | Selfrust-analyzer cargo defer-drop 1.3.0 impl#[GarbageCan] { senderrust-analyzer cargo defer-drop 1.3.0 GarbageCan#sender., handlerust-analyzer cargo defer-drop 1.3.0 GarbageCan#handle. } |
| 192 | } |
| 193 | |
| 194 | fn throw_awayrust-analyzer cargo defer-drop 1.3.0 impl#[GarbageCan]throw_away().<T: Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send# + 'static>(&self, value: T) { |
| 195 | // Only send to the garbage can if we're not currently in the garbage |
| 196 | // can; if we are, just drop it eagerly. |
| 197 | if threadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/::currentrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/current/current().().idrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/thread/impl#[Thread]id().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().self.handlerust-analyzer cargo defer-drop 1.3.0 GarbageCan#handle..threadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/join_handle/impl#[`JoinHandle<T>`]thread().().idrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/thread/impl#[Thread]id().() { |
| 198 | let boxed = Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T>`]new().(value); |
| 199 | self.senderrust-analyzer cargo defer-drop 1.3.0 GarbageCan#sender..sendrust-analyzer cargo crossbeam-channel 0.5.6 channel/impl#[`Sender<T>`]send().(boxed).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | #[cfg(test)] |
| 205 | mod testsrust-analyzer cargo defer-drop 1.3.0 tests/ { |
| 206 | use crossbeam_channelrust-analyzer cargo crossbeam-channel 0.5.6 crate/ as channelrust-analyzer cargo crossbeam-channel 0.5.6 crate/; |
| 207 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::{ |
| 208 | syncrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/::{Arcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/Arc#, Mutexrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/poison/mutex/Mutex#}, |
| 209 | threadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/, |
| 210 | timerust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/::Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#, |
| 211 | }; |
| 212 | |
| 213 | use craterust-analyzer cargo defer-drop 1.3.0 crate/::DeferDroprust-analyzer cargo defer-drop 1.3.0 DeferDrop#; |
| 214 | |
| 215 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 216 | fn testrust-analyzer cargo defer-drop 1.3.0 tests/test().() { |
| 217 | /// This struct, when dropped, reports the thread ID of its dropping |
| 218 | /// thread to the channel |
| 219 | struct ThreadReporterrust-analyzer cargo defer-drop 1.3.0 tests/ThreadReporter# { |
| 220 | chanrust-analyzer cargo defer-drop 1.3.0 tests/ThreadReporter#chan.: channelrust-analyzer cargo crossbeam-channel 0.5.6 crate/::Senderrust-analyzer cargo crossbeam-channel 0.5.6 channel/Sender#<threadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/::ThreadIdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/id/ThreadId#>, |
| 221 | } |
| 222 | |
| 223 | impl Droprust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/drop/Drop# for ThreadReporterrust-analyzer cargo defer-drop 1.3.0 tests/ThreadReporter# { |
| 224 | fn droprust-analyzer cargo defer-drop 1.3.0 tests/impl#[ThreadReporter][Drop]drop().(&mut self) { |
| 225 | self.chanrust-analyzer cargo defer-drop 1.3.0 tests/ThreadReporter#chan..sendrust-analyzer cargo crossbeam-channel 0.5.6 channel/impl#[`Sender<T>`]send().(threadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/::currentrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/current/current().().idrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/thread/impl#[Thread]id().()).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | let (sender, receiver) = channelrust-analyzer cargo crossbeam-channel 0.5.6 crate/::boundedrust-analyzer cargo crossbeam-channel 0.5.6 channel/bounded().(1); |
| 230 | let this_thread_id = threadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/::currentrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/current/current().().idrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/thread/impl#[Thread]id().(); |
| 231 | |
| 232 | let thing = DeferDroprust-analyzer cargo defer-drop 1.3.0 DeferDrop#::newrust-analyzer cargo defer-drop 1.3.0 impl#[`DeferDrop<T>`]new().(ThreadReporterrust-analyzer cargo defer-drop 1.3.0 tests/ThreadReporter# { chanrust-analyzer cargo defer-drop 1.3.0 tests/ThreadReporter#chan.: sender }); |
| 233 | droprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/drop().(thing); |
| 234 | |
| 235 | match receiver.recv_timeoutrust-analyzer cargo crossbeam-channel 0.5.6 channel/impl#[`Receiver<T>`]recv_timeout().(Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#::from_secsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]from_secs().(1)) { |
| 236 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(id) => assert_nerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_ne!!( |
| 237 | id, this_thread_id, |
| 238 | "thing wasn't dropped in a different thread" |
| 239 | ), |
| 240 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(_) => panicrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/panic!!("thing wasn't dropped within one second of being dropped"), |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | // Test that `DeferDrop` values that are sent into the dropper thread are |
| 245 | // dropped locally (that is, they don't re-send into the channel) |
| 246 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 247 | fn test_no_recursive_sendrust-analyzer cargo defer-drop 1.3.0 tests/test_no_recursive_send().() { |
| 248 | #[allow(dead_code)] |
| 249 | struct DropOrderRecorderrust-analyzer cargo defer-drop 1.3.0 tests/DropOrderRecorder#<T> { |
| 250 | idrust-analyzer cargo defer-drop 1.3.0 tests/DropOrderRecorder#id.: u32, |
| 251 | valuerust-analyzer cargo defer-drop 1.3.0 tests/DropOrderRecorder#value.: T, |
| 252 | recordrust-analyzer cargo defer-drop 1.3.0 tests/DropOrderRecorder#record.: Arcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/Arc#<Mutexrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/poison/mutex/Mutex#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u32>>>, |
| 253 | } |
| 254 | |
| 255 | impl<T> Droprust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/drop/Drop# for DropOrderRecorderrust-analyzer cargo defer-drop 1.3.0 tests/DropOrderRecorder#<T> { |
| 256 | fn droprust-analyzer cargo defer-drop 1.3.0 tests/impl#[`DropOrderRecorder<T>`][Drop]drop().(&mut self) { |
| 257 | self.recordrust-analyzer cargo defer-drop 1.3.0 tests/DropOrderRecorder#record..lockrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/poison/mutex/impl#[`Mutex<T>`]lock().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().().pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(self.idrust-analyzer cargo defer-drop 1.3.0 tests/DropOrderRecorder#id.) |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | let drop_order_record: Arcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/Arc#<Mutexrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/poison/mutex/Mutex#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u32>>> = Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default#::defaultrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/impl#[`Arc<T>`][Default]default().(); |
| 262 | |
| 263 | let value = DeferDroprust-analyzer cargo defer-drop 1.3.0 DeferDrop#::newrust-analyzer cargo defer-drop 1.3.0 impl#[`DeferDrop<T>`]new().(DropOrderRecorderrust-analyzer cargo defer-drop 1.3.0 tests/DropOrderRecorder# { |
| 264 | idrust-analyzer cargo defer-drop 1.3.0 tests/DropOrderRecorder#id.: 0, |
| 265 | recordrust-analyzer cargo defer-drop 1.3.0 tests/DropOrderRecorder#record.: drop_order_record.clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/impl#[`Arc<T, A>`][Clone]clone().(), |
| 266 | valuerust-analyzer cargo defer-drop 1.3.0 tests/DropOrderRecorder#value.: [ |
| 267 | DeferDroprust-analyzer cargo defer-drop 1.3.0 DeferDrop#::newrust-analyzer cargo defer-drop 1.3.0 impl#[`DeferDrop<T>`]new().(DropOrderRecorderrust-analyzer cargo defer-drop 1.3.0 tests/DropOrderRecorder# { |
| 268 | idrust-analyzer cargo defer-drop 1.3.0 tests/DropOrderRecorder#id.: 1, |
| 269 | recordrust-analyzer cargo defer-drop 1.3.0 tests/DropOrderRecorder#record.: drop_order_record.clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/impl#[`Arc<T, A>`][Clone]clone().(), |
| 270 | valuerust-analyzer cargo defer-drop 1.3.0 tests/DropOrderRecorder#value.: [ |
| 271 | DeferDroprust-analyzer cargo defer-drop 1.3.0 DeferDrop#::newrust-analyzer cargo defer-drop 1.3.0 impl#[`DeferDrop<T>`]new().(DropOrderRecorderrust-analyzer cargo defer-drop 1.3.0 tests/DropOrderRecorder# { |
| 272 | idrust-analyzer cargo defer-drop 1.3.0 tests/DropOrderRecorder#id.: 2, |
| 273 | recordrust-analyzer cargo defer-drop 1.3.0 tests/DropOrderRecorder#record.: drop_order_record.clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/impl#[`Arc<T, A>`][Clone]clone().(), |
| 274 | valuerust-analyzer cargo defer-drop 1.3.0 tests/DropOrderRecorder#value.: (), |
| 275 | }), |
| 276 | DeferDroprust-analyzer cargo defer-drop 1.3.0 DeferDrop#::newrust-analyzer cargo defer-drop 1.3.0 impl#[`DeferDrop<T>`]new().(DropOrderRecorderrust-analyzer cargo defer-drop 1.3.0 tests/DropOrderRecorder# { |
| 277 | idrust-analyzer cargo defer-drop 1.3.0 tests/DropOrderRecorder#id.: 3, |
| 278 | recordrust-analyzer cargo defer-drop 1.3.0 tests/DropOrderRecorder#record.: drop_order_record.clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/impl#[`Arc<T, A>`][Clone]clone().(), |
| 279 | valuerust-analyzer cargo defer-drop 1.3.0 tests/DropOrderRecorder#value.: (), |
| 280 | }), |
| 281 | ], |
| 282 | }), |
| 283 | DeferDroprust-analyzer cargo defer-drop 1.3.0 DeferDrop#::newrust-analyzer cargo defer-drop 1.3.0 impl#[`DeferDrop<T>`]new().(DropOrderRecorderrust-analyzer cargo defer-drop 1.3.0 tests/DropOrderRecorder# { |
| 284 | idrust-analyzer cargo defer-drop 1.3.0 tests/DropOrderRecorder#id.: 4, |
| 285 | recordrust-analyzer cargo defer-drop 1.3.0 tests/DropOrderRecorder#record.: drop_order_record.clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/impl#[`Arc<T, A>`][Clone]clone().(), |
| 286 | valuerust-analyzer cargo defer-drop 1.3.0 tests/DropOrderRecorder#value.: [ |
| 287 | DeferDroprust-analyzer cargo defer-drop 1.3.0 DeferDrop#::newrust-analyzer cargo defer-drop 1.3.0 impl#[`DeferDrop<T>`]new().(DropOrderRecorderrust-analyzer cargo defer-drop 1.3.0 tests/DropOrderRecorder# { |
| 288 | idrust-analyzer cargo defer-drop 1.3.0 tests/DropOrderRecorder#id.: 5, |
| 289 | recordrust-analyzer cargo defer-drop 1.3.0 tests/DropOrderRecorder#record.: drop_order_record.clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/impl#[`Arc<T, A>`][Clone]clone().(), |
| 290 | valuerust-analyzer cargo defer-drop 1.3.0 tests/DropOrderRecorder#value.: (), |
| 291 | }), |
| 292 | DeferDroprust-analyzer cargo defer-drop 1.3.0 DeferDrop#::newrust-analyzer cargo defer-drop 1.3.0 impl#[`DeferDrop<T>`]new().(DropOrderRecorderrust-analyzer cargo defer-drop 1.3.0 tests/DropOrderRecorder# { |
| 293 | idrust-analyzer cargo defer-drop 1.3.0 tests/DropOrderRecorder#id.: 6, |
| 294 | recordrust-analyzer cargo defer-drop 1.3.0 tests/DropOrderRecorder#record.: drop_order_record.clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/impl#[`Arc<T, A>`][Clone]clone().(), |
| 295 | valuerust-analyzer cargo defer-drop 1.3.0 tests/DropOrderRecorder#value.: (), |
| 296 | }), |
| 297 | ], |
| 298 | }), |
| 299 | ], |
| 300 | }); |
| 301 | |
| 302 | droprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/drop().(value); |
| 303 | |
| 304 | loop { |
| 305 | threadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/::yield_nowrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/functions/yield_now().(); |
| 306 | let lock = drop_order_record.lockrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/poison/mutex/impl#[`Mutex<T>`]lock().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 307 | if lock.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().>=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().7 { |
| 308 | break; |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | let lock = drop_order_record.lockrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/poison/mutex/impl#[`Mutex<T>`]lock().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 313 | |
| 314 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(lock.as_slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]as_slice().(), [0, 1, 2, 3, 4, 5, 6]) |
| 315 | } |
| 316 | } |