diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
commit | 2aa4a82499d4becd2284cdb482213d541b8804dd (patch) | |
tree | b80bf8bf13c3766139fbacc530efd0dd9d54394c /third_party/rust/rental/tests/simple_mut.rs | |
parent | Initial commit. (diff) | |
download | firefox-upstream.tar.xz firefox-upstream.zip |
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/rental/tests/simple_mut.rs')
-rw-r--r-- | third_party/rust/rental/tests/simple_mut.rs | 67 |
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); + } +} |