chiark / gitweb /
demo borrowed panic
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 16 Nov 2024 18:53:53 +0000 (18:53 +0000)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 16 Nov 2024 18:53:53 +0000 (18:53 +0000)
src/test.rs
src/test/demo.rs

index 7b1743004204c9087b9092e697fc4c680f61d681..e1b133ae5c0546e840de6d7c1aa6339eb83d18b5 100644 (file)
@@ -1,6 +1,7 @@
 
 use super::*;
 use fmt::Write as _;
+use std::panic::{AssertUnwindSafe, catch_unwind, resume_unwind};
 
 mod demo;
 use demo::{List, HeadAndTail};
@@ -45,6 +46,16 @@ fn demo() {
     l.check_consistency();
     assert_eq!(l.head_and_tail_mut(), H::Both(&mut s("one"), &mut s("ho")));
 
+    catch_unwind(AssertUnwindSafe(|| {
+        l.temporarily_prepend(s("t"), |l| {
+            l.check_consistency();
+            assert_eq!(l.front(), Some(&s("t")));
+            panic!("yikes");
+        });
+    })).unwrap_err();
+    l.check_consistency();
+    assert_eq!(l.head_and_tail_mut(), H::Both(&mut s("one"), &mut s("ho")));
+
     write!(l.front_mut().unwrap(), "!").unwrap();
     assert_eq!(l.pop_front(), Some(s("one!")));   l.check_consistency();
     assert_eq!(l.front(), Some(&s("hi")));        l.check_consistency();
index da3304d233203acbc07fac7f3f153a2654e1ddbd..4054b5f3ce86f1c062139f2742886e5520aa34d7 100644 (file)
@@ -120,8 +120,6 @@ impl<T: Debug> List<T> {
         data: T,
         then: impl FnOnce(&Self) -> R,
     ) -> R {
-        use std::panic::{AssertUnwindSafe, catch_unwind, resume_unwind};
-
         let mut tmp_node = Node {
             data,
             next: None,