Debian Codegraph @

src/lib.rs

raw symbols SCIP
1 //! Temporarily take ownership of a value at a mutable location, and replace it with a new value
2 //! based on the old one.
3 //!
4 //! <p style="font-family: 'Fira Sans',sans-serif;padding:0.3em 0"><strong>
5 //! <a href="https://crates.io/crates/replace_with">📦&nbsp;&nbsp;Crates.io</a>&nbsp;&nbsp;│&nbsp;&nbsp;<a href="https://github.com/alecmocatta/replace_with">📑&nbsp;&nbsp;GitHub</a>&nbsp;&nbsp;│&nbsp;&nbsp;<a href="https://constellation.zulipchat.com/#narrow/stream/213236-subprojects">💬&nbsp;&nbsp;Chat</a>
6 //! </strong></p>
7 //!
8 //! This crate provides the function [`replace_with()`], which is like [`std::mem::replace()`]
9 //! except it allows the replacement value to be mapped from the original value.
10 //!
11 //! See [RFC 1736](https://github.com/rust-lang/rfcs/pull/1736) for a lot of discussion as to its
12 //! merits. It was never merged, and the desired ability to temporarily move out of `&mut T` doesn't
13 //! exist yet, so this crate is my interim solution.
14 //!
15 //! It's very akin to [`take_mut`](https://github.com/Sgeo/take_mut), though uses `Drop` instead of
16 //! [`std::panic::catch_unwind()`] to react to unwinding, which avoids the optimisation barrier of
17 //! calling the `extern "C" __rust_maybe_catch_panic()`. As such it's up to ∞x faster. The API also
18 //! attempts to make slightly more explicit the behavior on panic – [`replace_with()`] accepts two
19 //! closures such that aborting in the "standard case" where the mapping closure (`FnOnce(T) -> T`)
20 //! panics (as [`take_mut::take()`](https://docs.rs/take_mut/0.2.2/take_mut/fn.take.html) does) is
21 //! avoided. If the second closure (`FnOnce() -> T`) panics, however, then it does indeed abort.
22 //! The "abort on first panic" behaviour is available with [`replace_with_or_abort()`].
23 //!
24 //! # Example
25 //!
26 //! Consider this motivating example:
27 //!
28 //! ```compile_fail
29 //! # use replace_with::*;
30 //! enum States {
31 //! A(String),
32 //! B(String),
33 //! }
34 //!
35 //! impl States {
36 //! fn poll(&mut self) {
37 //! // error[E0507]: cannot move out of borrowed content
38 //! *self = match *self {
39 //! // ^^^^^ cannot move out of borrowed content
40 //! States::A(a) => States::B(a),
41 //! States::B(a) => States::A(a),
42 //! };
43 //! }
44 //! }
45 //! ```
46 //!
47 //! Depending on context this can be quite tricky to work around. With this crate, however:
48 //!
49 //! ```
50 //! # use replace_with::*;
51 //! enum States {
52 //! A(String),
53 //! B(String),
54 //! }
55 //!
56 //! # #[cfg(any(feature = "std", feature = "nightly"))]
57 //! impl States {
58 //! fn poll(&mut self) {
59 //! replace_with_or_abort(self, |self_| match self_ {
60 //! States::A(a) => States::B(a),
61 //! States::B(a) => States::A(a),
62 //! });
63 //! }
64 //! }
65 //! ```
66 //!
67 //! Huzzah!
68 //!
69 //! # `no_std`
70 //!
71 //! To use `replace_with` with `no_std` you have to disable the `std` feature, which is active by
72 //! default, by specifying your dependency to it like this:
73 //!
74 //! ```toml
75 //! # Cargo.toml
76 //!
77 //! replace_with = { version = "0.1", default-features = false }
78 //! ```
79 //!
80 //! The `nightly` feature can be enabled to use [`core::intrinsics::abort()`](https://doc.rust-lang.org/core/intrinsics/fn.abort.html)
81 //! instead of triggering an abort via [`std::process::abort()`](https://doc.rust-lang.org/std/process/fn.abort.html)
82 //! or [`extern "C" fn abort() { panic!() } abort()`](https://doc.rust-lang.org/reference/items/functions.html#r-items.fn.extern.abort).
83
84 #![cfg_attr(not(feature = "std"), no_std)]
85 #![cfg_attr(feature = "nightly", feature(core_intrinsics))]
86 #![doc(html_root_url = "https://docs.rs/replace_with/0.1.8")]
87 #![warn(
88 missing_copy_implementations,
89 missing_debug_implementations,
90 missing_docs,
91 trivial_casts,
92 trivial_numeric_casts,
93 unused_import_braces,
94 unused_qualifications,
95 unused_results,
96 // clippy::pedantic
97 )]
98 // #![allow(clippy::inline_always)]
99
100 #[cfg(not(feature = "std"))]
101 use core as std;
102
103 use std::{mem, ptr};
104
105 #[cfg(feature = "nightly")]
106 #[inline(always)]
107 fn abort() -> ! {
108 unsafe { std::intrinsics::abort() }
109 }
110
111 #[cfg(all(not(feature = "nightly"), feature = "std"))]
112 use std::process::abort;
113
114 // TODO: https://github.com/rust-lang/rfcs/issues/2512
115 #[cfg(all(not(feature = "nightly"), not(feature = "std")))]
116 #[inline(always)]
117 fn abort() -> ! {
118 if cfg!(panic = "abort") {
119 panic!()
120 } else {
121 // https://doc.rust-lang.org/reference/items/functions.html#r-items.fn.extern.abort
122 #[inline(always)]
123 extern "C" fn abort() -> ! {
124 panic!();
125 }
126 on_unwind(|| abort(), || loop {})
127 }
128 }
129
130 // TODO: https://github.com/rust-lang/rust/issues/130338
131 #[inline(always)]
132 fn abort_unwind<F: FnOnce() -> R, R>(f: F) -> R {
133 on_unwind(f, || abort())
134 }
135
136 struct OnDrop<F: FnOnce()>(mem::ManuallyDrop<F>);
137 impl<F: FnOnce()> Drop for OnDrop<F> {
138 #[inline(always)]
139 fn drop(&mut self) {
140 (unsafe { ptr::read(&*self.0) })();
141 }
142 }
143
144 #[inline(always)]
145 fn on_unwind_inner<F: FnOnce() -> T, T, P: FnOnce()>(f: F, p: P) -> (T, P) {
146 if cfg!(panic = "abort") {
147 return (f(), p);
148 }
149 let p = OnDrop(mem::ManuallyDrop::new(p));
150 let t = f();
151 let mut p = mem::ManuallyDrop::new(p);
152 (t, unsafe { mem::ManuallyDrop::take(&mut p.0) })
153 }
154
155 #[doc(hidden)]
156 #[inline(always)]
157 pub fn on_unwind<F: FnOnce() -> T, T, P: FnOnce()>(f: F, p: P) -> T {
158 on_unwind_inner(f, p).0
159 }
160
161 #[doc(hidden)]
162 #[inline(always)]
163 pub fn on_return_or_unwind<F: FnOnce() -> T, T, P: FnOnce()>(f: F, p: P) -> T {
164 let _p = OnDrop(mem::ManuallyDrop::new(p));
165 f()
166 }
167
168 /// Temporarily takes ownership of a value at a mutable location, and replace it with a new value
169 /// based on the old one.
170 ///
171 /// We move out of the reference temporarily, to apply a closure `f`, returning a new value, which
172 /// is then placed at the original value's location.
173 ///
174 /// # An important note
175 ///
176 /// On panic (or to be more precise, unwinding) of the closure `f`, `default` will be called to
177 /// provide a replacement value. `default` should not panic – doing so will abort the process.
178 ///
179 /// # Example
180 ///
181 /// ```
182 /// # use replace_with::*;
183 /// enum States {
184 /// A(String),
185 /// B(String),
186 /// }
187 ///
188 /// impl States {
189 /// fn poll(&mut self) {
190 /// replace_with(
191 /// self,
192 /// || States::A(String::new()),
193 /// |self_| match self_ {
194 /// States::A(a) => States::B(a),
195 /// States::B(a) => States::A(a),
196 /// },
197 /// );
198 /// }
199 /// }
200 /// ```
201 #[inline]
202 pub fn replace_with<T, D: FnOnce() -> T, F: FnOnce(T) -> T>(dest: &mut T, default: D, f: F) {
203 unsafe {
204 let old = ptr::read(dest);
205 let (new, _default) =
206 on_unwind_inner(move || f(old), || ptr::write(dest, abort_unwind(default)));
207 ptr::write(dest, new);
208 }
209 }
210
211 /// Temporarily takes ownership of a value at a mutable location, and replace it with a new value
212 /// based on the old one. Replaces with [`Default::default()`] on panic.
213 ///
214 /// We move out of the reference temporarily, to apply a closure `f`, returning a new value, which
215 /// is then placed at the original value's location.
216 ///
217 /// # An important note
218 ///
219 /// On panic (or to be more precise, unwinding) of the closure `f`, `T::default()` will be called to
220 /// provide a replacement value. `T::default()` should not panic – doing so will abort the process.
221 ///
222 /// Equivalent to `replace_with(dest, T::default, f)`.
223 ///
224 /// Differs from `*dest = mem::replace(dest, Default::default())` in that `Default::default()` will
225 /// only be called on panic.
226 ///
227 /// # Example
228 ///
229 /// ```
230 /// # use replace_with::*;
231 /// enum States {
232 /// A(String),
233 /// B(String),
234 /// }
235 ///
236 /// impl Default for States {
237 /// fn default() -> Self {
238 /// States::A(String::new())
239 /// }
240 /// }
241 ///
242 /// impl States {
243 /// fn poll(&mut self) {
244 /// replace_with_or_default(self, |self_| match self_ {
245 /// States::A(a) => States::B(a),
246 /// States::B(a) => States::A(a),
247 /// });
248 /// }
249 /// }
250 /// ```
251 #[inline]
252 pub fn replace_with_or_default<T: Default, F: FnOnce(T) -> T>(dest: &mut T, f: F) {
253 replace_with(dest, T::default, f);
254 }
255
256 /// Temporarily takes ownership of a value at a mutable location, and replace it with a new value
257 /// based on the old one. Aborts on panic.
258 ///
259 /// We move out of the reference temporarily, to apply a closure `f`, returning a new value, which
260 /// is then placed at the original value's location.
261 ///
262 /// # An important note
263 ///
264 /// On panic (or to be more precise, unwinding) of the closure `f`, the process will **abort** to
265 /// avoid returning control while `dest` is in a potentially invalid state.
266 ///
267 /// If this behaviour is undesirable, use [`replace_with`] or [`replace_with_or_default`].
268 ///
269 /// Equivalent to `replace_with(dest, || process::abort(), f)`.
270 ///
271 /// # Example
272 ///
273 /// ```
274 /// # use replace_with::*;
275 /// enum States {
276 /// A(String),
277 /// B(String),
278 /// }
279 ///
280 /// # #[cfg(any(feature = "std", feature = "nightly"))]
281 /// impl States {
282 /// fn poll(&mut self) {
283 /// replace_with_or_abort(self, |self_| match self_ {
284 /// States::A(a) => States::B(a),
285 /// States::B(a) => States::A(a),
286 /// });
287 /// }
288 /// }
289 /// ```
290 #[inline]
291 pub fn replace_with_or_abort<T, F: FnOnce(T) -> T>(dest: &mut T, f: F) {
292 replace_with(dest, || abort(), f);
293 }
294
295 /// Temporarily takes ownership of a value at a mutable location, and replace it with a new value
296 /// based on the old one. Aborts on panic.
297 ///
298 /// We move out of the reference temporarily, to apply a closure `f`, returning a new value, which
299 /// is then placed at the original value's location.
300 ///
301 /// # An important note
302 ///
303 /// On panic (or to be more precise, unwinding) of the closure `f`, the process will **abort** to
304 /// avoid returning control while `dest` is in a potentially invalid state.
305 ///
306 /// Unlike for `replace_with_or_abort()` users of `replace_with_or_abort_unchecked()` are expected
307 /// to have `features = ["panic_abort", …]` defined in `Cargo.toml`
308 /// and `panic = "abort"` defined in their profile for it to behave semantically correct:
309 ///
310 /// ```toml
311 /// # Cargo.toml
312 ///
313 /// [profile.debug]
314 /// panic = "abort"
315 ///
316 /// [profile.release]
317 /// panic = "abort"
318 /// ```
319 ///
320 /// # Safety
321 ///
322 /// It is crucial to only ever use this function having defined `panic = "abort"`, or else bad
323 /// things may happen. It's *up to you* to uphold this invariant!
324 ///
325 /// If this behaviour is undesirable, use [`replace_with`] or [`replace_with_or_default`].
326 ///
327 /// Equivalent to `replace_with(dest, || process::abort(), f)`.
328 ///
329 /// # Example
330 ///
331 /// ```
332 /// # use replace_with::*;
333 /// enum States {
334 /// A(String),
335 /// B(String),
336 /// }
337 ///
338 /// impl States {
339 /// fn poll(&mut self) {
340 /// unsafe {
341 /// replace_with_or_abort_unchecked(self, |self_| match self_ {
342 /// States::A(a) => States::B(a),
343 /// States::B(a) => States::A(a),
344 /// });
345 /// }
346 /// }
347 /// }
348 /// ```
349 ///
350 #[inline]
351 #[cfg(feature = "panic_abort")]
352 #[deprecated = "Use replace_with_or_abort() instead, which used cfg!(panic = \"abort\") to guarentee soundness"]
353 pub unsafe fn replace_with_or_abort_unchecked<T, F: FnOnce(T) -> T>(dest: &mut T, f: F) {
354 ptr::write(dest, f(ptr::read(dest)));
355 }
356
357 /// Temporarily takes ownership of a value at a mutable location, and replace it with a new value
358 /// based on the old one. Lets the closure return a custom value as well.
359 ///
360 /// We move out of the reference temporarily, to apply a closure `f`, returning a new value, which
361 /// is then placed at the original value's location.
362 ///
363 /// This is effectively the same function as [`replace_with`], but it lets the closure return a
364 /// custom value as well.
365 ///
366 /// # An important note
367 ///
368 /// On panic (or to be more precise, unwinding) of the closure `f`, `default` will be called to
369 /// provide a replacement value. `default` should not panic – doing so will abort the process.
370 ///
371 /// # Example
372 ///
373 /// ```
374 /// # use replace_with::*;
375 ///
376 /// fn take<T>(option: &mut Option<T>) -> Option<T> {
377 /// replace_with_and_return(option, || None, |option| (option, None))
378 /// }
379 ///
380 /// let mut opt = Some(3);
381 /// assert_eq!(take(&mut opt), Some(3));
382 /// assert_eq!(take(&mut opt), None);
383 /// ```
384 #[inline]
385 pub fn replace_with_and_return<T, U, D: FnOnce() -> T, F: FnOnce(T) -> (U, T)>(
386 dest: &mut T, default: D, f: F,
387 ) -> U {
388 unsafe {
389 let old = ptr::read(dest);
390 let ((res, new), _default) =
391 on_unwind_inner(move || f(old), || ptr::write(dest, abort_unwind(default)));
392 ptr::write(dest, new);
393 res
394 }
395 }
396
397 /// Temporarily takes ownership of a value at a mutable location, and replace it with a new value
398 /// based on the old one. Replaces with [`Default::default()`] on panic.
399 /// Lets the closure return a custom value as well.
400 ///
401 /// We move out of the reference temporarily, to apply a closure `f`, returning a new value, which
402 /// is then placed at the original value's location.
403 ///
404 /// This is effectively the same function as [`replace_with_or_default`], but it lets the closure
405 /// return a custom value as well.
406 ///
407 /// # An important note
408 ///
409 /// On panic (or to be more precise, unwinding) of the closure `f`, `T::default()` will be called to
410 /// provide a replacement value. `T::default()` should not panic – doing so will abort the process.
411 ///
412 /// Equivalent to `replace_with_and_return(dest, T::default, f)`.
413 ///
414 /// # Example
415 ///
416 /// ```
417 /// # use replace_with::*;
418 ///
419 /// fn take<T>(option: &mut Option<T>) -> Option<T> {
420 /// replace_with_or_default_and_return(option, |option| (option, None))
421 /// }
422 /// ```
423 #[inline]
424 pub fn replace_with_or_default_and_return<T: Default, U, F: FnOnce(T) -> (U, T)>(
425 dest: &mut T, f: F,
426 ) -> U {
427 replace_with_and_return(dest, T::default, f)
428 }
429
430 /// Temporarily takes ownership of a value at a mutable location, and replace it with a new value
431 /// based on the old one. Aborts on panic. Lets the closure return a custom value as well.
432 ///
433 /// We move out of the reference temporarily, to apply a closure `f`, returning a new value, which
434 /// is then placed at the original value's location.
435 ///
436 /// This is effectively the same function as [`replace_with_or_abort`], but it lets the closure
437 /// return a custom value as well.
438 ///
439 /// # An important note
440 ///
441 /// On panic (or to be more precise, unwinding) of the closure `f`, the process will **abort** to
442 /// avoid returning control while `dest` is in a potentially invalid state.
443 ///
444 /// If this behaviour is undesirable, use [`replace_with_and_return`] or
445 /// [`replace_with_or_default_and_return`] instead.
446 ///
447 /// Equivalent to `replace_with_and_return(dest, || process::abort(), f)`.
448 ///
449 /// # Example
450 ///
451 /// ```
452 /// # use replace_with::*;
453 ///
454 /// fn take<T>(option: &mut Option<T>) -> Option<T> {
455 /// replace_with_or_abort_and_return(option, |option| (option, None))
456 /// }
457 /// ```
458 #[inline]
459 pub fn replace_with_or_abort_and_return<T, U, F: FnOnce(T) -> (U, T)>(dest: &mut T, f: F) -> U {
460 replace_with_and_return(dest, || abort(), f)
461 }
462
463 /// Temporarily takes ownership of a value at a mutable location, and replace it with a new value
464 /// based on the old one. Aborts on panic. Lets the closure return a custom value as well.
465 ///
466 /// We move out of the reference temporarily, to apply a closure `f`, returning a new value, which
467 /// is then placed at the original value's location.
468 ///
469 /// This is effectively the same function as [`replace_with_or_abort_unchecked`], but it lets the
470 /// closure return a custom value as well.
471 ///
472 /// # An important note
473 ///
474 /// On panic (or to be more precise, unwinding) of the closure `f`, the process will **abort** to
475 /// avoid returning control while `dest` is in a potentially invalid state.
476 ///
477 /// Unlike for `replace_with_or_abort()` users of `replace_with_or_abort_unchecked()` are expected
478 /// to have `features = ["panic_abort", …]` defined in `Cargo.toml`
479 /// and `panic = "abort"` defined in their profile for it to behave semantically correct:
480 ///
481 /// ```toml
482 /// # Cargo.toml
483 ///
484 /// [profile.debug]
485 /// panic = "abort"
486 ///
487 /// [profile.release]
488 /// panic = "abort"
489 /// ```
490 ///
491 /// # Safety
492 ///
493 /// It is crucial to only ever use this function having defined `panic = "abort"`, or else bad
494 /// things may happen. It's *up to you* to uphold this invariant!
495 ///
496 /// If this behaviour is undesirable, use [`replace_with_and_return`] or
497 /// [`replace_with_or_default_and_return`].
498 ///
499 /// Equivalent to `replace_with_and_return(dest, || process::abort(), f)`.
500 ///
501 /// # Example
502 ///
503 /// ```
504 /// # use replace_with::*;
505 ///
506 /// unsafe fn take<T>(option: &mut Option<T>) -> Option<T> {
507 /// replace_with_or_abort_and_return_unchecked(option, |option| (option, None))
508 /// }
509 /// ```
510 #[inline]
511 #[cfg(feature = "panic_abort")]
512 #[deprecated = "Use replace_with_or_abort_and_return() instead, which used cfg!(panic = \"abort\") to guarentee soundness"]
513 pub unsafe fn replace_with_or_abort_and_return_unchecked<T, U, F: FnOnce(T) -> (U, T)>(
514 dest: &mut T, f: F,
515 ) -> U {
516 let (res, new) = f(ptr::read(dest));
517 ptr::write(dest, new);
518 res
519 }
520
521 #[cfg(test)]
522 mod test {
523 /// https://github.com/alecmocatta/replace_with/issues/18 replace_with[_and_return] has
524 /// undefined behavior if default panics.
525 #[test]
526 #[ignore = "should abort, cc https://github.com/rust-lang/rust/issues/67650"]
527 #[cfg(feature = "std")]
528 fn default_panic() {
529 let mut s = String::from("abc");
530 replace_with(
531 &mut s,
532 move || {
533 panic!();
534 },
535 |s| {
536 drop(s);
537 panic!();
538 },
539 );
540 }
541
542 /// https://github.com/alecmocatta/replace_with/issues/21 replace_with doesn't write the return
543 /// value before dropping the default closure, causing a use after free if the drop glue of the
544 /// latter panics
545 #[test]
546 #[should_panic]
547 #[cfg(feature = "std")]
548 fn default_drop_panic() {
549 struct OnDropPanic();
550 impl Drop for OnDropPanic {
551 fn drop(&mut self) {
552 panic!();
553 }
554 }
555
556 let mut s = String::from("abc");
557 let panicking_drop = OnDropPanic();
558 replace_with(
559 &mut s,
560 // The code inside this closure never runs, but because `panicking_drop` is owned by the
561 // closure, dropping this closure causes a panic.
562 move || {
563 drop(panicking_drop);
564 unreachable!()
565 },
566 |s| {
567 drop(s);
568 String::new()
569 },
570 );
571 }
572
573 // These functions copied from https://github.com/Sgeo/take_mut/blob/1bd70d842c6febcd16ec1fe3a954a84032b89f52/src/lib.rs#L102-L147
574
575 // Copyright (c) 2016 Sgeo
576
577 // Permission is hereby granted, free of charge, to any person obtaining a copy
578 // of this software and associated documentation files (the "Software"), to deal
579 // in the Software without restriction, including without limitation the rights
580 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
581 // copies of the Software, and to permit persons to whom the Software is
582 // furnished to do so, subject to the following conditions:
583
584 // The above copyright notice and this permission notice shall be included in all
585 // copies or substantial portions of the Software.
586
587 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
588 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
589 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
590 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
591 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
592 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
593 // SOFTWARE.
594
595 use super::*;
596
597 #[test]
598 fn it_works_recover() {
599 #[derive(PartialEq, Eq, Debug)]
600 enum Foo {
601 A,
602 B,
603 }
604 impl Drop for Foo {
605 #[cfg(feature = "std")]
606 fn drop(&mut self) {
607 match *self {
608 Foo::A => println!("Foo::A dropped"),
609 Foo::B => println!("Foo::B dropped"),
610 }
611 }
612
613 #[cfg(not(feature = "std"))]
614 fn drop(&mut self) {
615 match *self {
616 Foo::A | Foo::B => (),
617 }
618 }
619 }
620 let mut quax = Foo::A;
621 replace_with(
622 &mut quax,
623 || Foo::A,
624 |f| {
625 drop(f);
626 Foo::B
627 },
628 );
629 assert_eq!(&quax, &Foo::B);
630
631 let n = replace_with_and_return(
632 &mut quax,
633 || Foo::B,
634 |f| {
635 drop(f);
636 (3, Foo::A)
637 },
638 );
639 assert_eq!(n, 3);
640 assert_eq!(&quax, &Foo::A);
641 }
642
643 #[test]
644 #[cfg(feature = "std")]
645 fn it_works_recover_panic() {
646 use std::panic;
647
648 #[derive(PartialEq, Eq, Debug)]
649 enum Foo {
650 A,
651 B,
652 C,
653 }
654 impl Drop for Foo {
655 fn drop(&mut self) {
656 match *self {
657 Foo::A => println!("Foo::A dropped"),
658 Foo::B => println!("Foo::B dropped"),
659 Foo::C => println!("Foo::C dropped"),
660 }
661 }
662 }
663 let mut quax = Foo::A;
664
665 let res = panic::catch_unwind(panic::AssertUnwindSafe(|| {
666 replace_with(
667 &mut quax,
668 || Foo::C,
669 |f| {
670 drop(f);
671 panic!("panic");
672 #[allow(unreachable_code)]
673 Foo::B
674 },
675 );
676 }));
677
678 assert!(res.is_err());
679 assert_eq!(&quax, &Foo::C);
680 }
681 }