summaryrefslogtreecommitdiffstats
path: root/third_party/rust/rental/tests/simple_mut.rs
diff options
context:
space:
mode:
Diffstat (limited to 'third_party/rust/rental/tests/simple_mut.rs')
-rw-r--r--third_party/rust/rental/tests/simple_mut.rs67
1 files changed, 67 insertions, 0 deletions
diff --git a/third_party/rust/rental/tests/simple_mut.rs b/third_party/rust/rental/tests/simple_mut.rs
new file mode 100644
index 0000000000..c7af358998
--- /dev/null
+++ b/third_party/rust/rental/tests/simple_mut.rs
@@ -0,0 +1,67 @@
+#[macro_use]
+extern crate rental;
+
+
+pub struct Foo {
+ i: i32,
+}
+
+impl Foo {
+ fn try_borrow_mut(&mut self) -> Result<&mut i32, ()> { Ok(&mut self.i) }
+ fn fail_borrow_mut(&mut self) -> Result<&mut i32, ()> { Err(()) }
+}
+
+
+rental! {
+ mod rentals {
+ use super::*;
+
+ #[rental_mut]
+ pub struct SimpleMut {
+ foo: Box<Foo>,
+ iref: &'foo mut i32,
+ }
+ }
+}
+
+
+#[test]
+fn new() {
+ let foo = Foo { i: 5 };
+ let _ = rentals::SimpleMut::new(Box::new(foo), |foo| &mut foo.i);
+
+ let foo = Foo { i: 5 };
+ let sm = rentals::SimpleMut::try_new(Box::new(foo), |foo| foo.try_borrow_mut());
+ assert!(sm.is_ok());
+
+ let foo = Foo { i: 5 };
+ let sm = rentals::SimpleMut::try_new(Box::new(foo), |foo| foo.fail_borrow_mut());
+ assert!(sm.is_err());
+}
+
+
+#[test]
+fn read() {
+ let foo = Foo { i: 5 };
+
+ let sm = rentals::SimpleMut::new(Box::new(foo), |foo| &mut foo.i);
+ let i: i32 = sm.rent(|iref| **iref);
+ assert_eq!(i, 5);
+
+ let iref: &i32 = sm.ref_rent(|iref| *iref);
+ assert_eq!(*iref, 5);
+}
+
+
+#[test]
+fn write() {
+ let foo = Foo { i: 5 };
+
+ let mut sm = rentals::SimpleMut::new(Box::new(foo), |foo| &mut foo.i);
+
+ {
+ let iref: &mut i32 = sm.ref_rent_mut(|iref| *iref);
+ *iref = 12;
+ assert_eq!(*iref, 12);
+ }
+}