about summary refs log tree commit diff
path: root/examples/defer-with-error.rs
diff options
context:
space:
mode:
authorVincent Ambo <tazjin@gmail.com>2017-10-22T15·22+0200
committerVincent Ambo <tazjin@gmail.com>2017-10-22T15·22+0200
commitff7b5b42bf3d761cbf28e56f30796059ef94e271 (patch)
tree3ae80d990c4489fd75fb1feceea0cf2b956a12cc /examples/defer-with-error.rs
parent5eec09334bd17ebb28a57021b56a6e687f433528 (diff)
feat: Add example with error return
Diffstat (limited to 'examples/defer-with-error.rs')
-rw-r--r--examples/defer-with-error.rs70
1 files changed, 70 insertions, 0 deletions
diff --git a/examples/defer-with-error.rs b/examples/defer-with-error.rs
new file mode 100644
index 000000000000..26d56d77cf1b
--- /dev/null
+++ b/examples/defer-with-error.rs
@@ -0,0 +1,70 @@
+// Go's defer in Rust, with error value return.
+
+use std::rc::Rc;
+use std::sync::RwLock;
+
+struct Defer<F: Fn()> {
+    f: F
+}
+
+impl <F: Fn()> Drop for Defer<F> {
+    fn drop(&mut self) {
+        (self.f)()
+    }
+}
+
+// Only added this for Go-syntax familiarity ;-)
+fn  defer<F: Fn()>(f: F) -> Defer<F> {
+    Defer { f }
+}
+
+// Convenience type synonym. This is a reference-counted smart pointer to
+// a shareable, mutable variable.
+// Rust does not allow willy-nilly mutation of shared variables, so explicit
+// write-locking must be performed.
+type ErrorHandle<T> = Rc<RwLock<Option<T>>>;
+
+///////////////////
+// Usage example //
+///////////////////
+
+#[derive(Debug)] // Debug trait for some default way to print the type.
+enum Error { DropError }
+
+fn main() {
+    // Create a place to store the error.
+    let drop_err: ErrorHandle<Error> = Default::default(); // create empty error
+
+    // Introduce an arbitrary scope block (so that we still have control after
+    // the defer runs):
+    {
+        let mut i = 1;
+
+        // Rc types are safe to clone and share for multiple ownership.
+        let err_handle = drop_err.clone();
+
+        // Call defer and let the closure own the cloned handle to the error:
+        let token = defer(move || {
+            // do something!
+            println!("Value is: {}", i);
+
+            // ... oh no, it went wrong!
+            *err_handle.write().unwrap() = Some(Error::DropError);
+        });
+
+        i += 1;
+        println!("Value is: {}", i);
+
+        // token goes out of scope here - drop() is called.
+    }
+
+    match *drop_err.read().unwrap() {
+        Some(ref err) => println!("Oh no, an error occured: {:?}!", err),
+        None => println!("Phew, everything went well.")
+    };
+}
+
+// Prints:
+// Value is: 2
+// Value is: 1
+// Oh no, an error occured: DropError!