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 | |
parent | Initial commit. (diff) | |
download | firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.tar.xz firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.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')
-rw-r--r-- | third_party/rust/rental/tests/clone.rs | 50 | ||||
-rw-r--r-- | third_party/rust/rental/tests/complex.rs | 115 | ||||
-rw-r--r-- | third_party/rust/rental/tests/complex_mut.rs | 136 | ||||
-rw-r--r-- | third_party/rust/rental/tests/covariant.rs | 52 | ||||
-rw-r--r-- | third_party/rust/rental/tests/debug.rs | 39 | ||||
-rw-r--r-- | third_party/rust/rental/tests/drop_order.rs | 129 | ||||
-rw-r--r-- | third_party/rust/rental/tests/generic.rs | 53 | ||||
-rw-r--r-- | third_party/rust/rental/tests/lt_params.rs | 65 | ||||
-rw-r--r-- | third_party/rust/rental/tests/map.rs | 42 | ||||
-rw-r--r-- | third_party/rust/rental/tests/simple_mut.rs | 67 | ||||
-rw-r--r-- | third_party/rust/rental/tests/simple_ref.rs | 103 | ||||
-rw-r--r-- | third_party/rust/rental/tests/string.rs | 30 | ||||
-rw-r--r-- | third_party/rust/rental/tests/subrental.rs | 88 | ||||
-rw-r--r-- | third_party/rust/rental/tests/target_ty_hack.rs | 54 | ||||
-rw-r--r-- | third_party/rust/rental/tests/trait.rs | 33 | ||||
-rw-r--r-- | third_party/rust/rental/tests/unused.rs | 28 | ||||
-rw-r--r-- | third_party/rust/rental/tests/vec_slice.rs | 47 |
17 files changed, 1131 insertions, 0 deletions
diff --git a/third_party/rust/rental/tests/clone.rs b/third_party/rust/rental/tests/clone.rs new file mode 100644 index 0000000000..043f84a895 --- /dev/null +++ b/third_party/rust/rental/tests/clone.rs @@ -0,0 +1,50 @@ +#[macro_use] +extern crate rental; + + +pub struct Foo { + i: i32, +} + +pub struct Bar<'i> { + iref: &'i i32, + misc: i32, +} + +impl <'i> Clone for Bar<'i> { + fn clone (&self) -> Self { + Bar{ + iref: Clone::clone(&self.iref), + misc: Clone::clone(&self.misc), + } + } +} + + +rental! { + mod rentals { + use super::*; + use std::sync::Arc; + + #[rental(clone)] + pub struct FooClone { + foo: Arc<Foo>, + fr: Bar<'foo>, + } + } +} + + +#[test] +fn clone() { + use std::sync::Arc; + + let foo = Foo { i: 5 }; + let rf = rentals::FooClone::new(Arc::new(foo), |foo| Bar{ iref: &foo.i, misc: 12 }); + assert_eq!(5, rf.rent(|f| *f.iref)); + + let rfc = rf.clone(); + assert_eq!(5, rfc.rent(|f| *f.iref)); +} + + diff --git a/third_party/rust/rental/tests/complex.rs b/third_party/rust/rental/tests/complex.rs new file mode 100644 index 0000000000..187e976807 --- /dev/null +++ b/third_party/rust/rental/tests/complex.rs @@ -0,0 +1,115 @@ +#[macro_use] +extern crate rental; + + +pub struct Foo { + i: i32, +} + +pub struct Bar<'a> { + foo: &'a Foo, +} + +pub struct Baz<'a: 'b, 'b> { + bar: &'b Bar<'a> +} + +pub struct Qux<'a: 'b, 'b: 'c, 'c> { + baz: &'c Baz<'a, 'b> +} + +pub struct Xyzzy<'a: 'b, 'b: 'c, 'c: 'd, 'd> { + qux: &'d Qux<'a, 'b, 'c> +} + + +impl Foo { + pub fn borrow<'a>(&'a self) -> Bar<'a> { Bar { foo: self } } + pub fn try_borrow<'a>(&'a self) -> Result<Bar<'a>, ()> { Ok(Bar { foo: self }) } + pub fn fail_borrow<'a>(&'a self) -> Result<Bar<'a>, ()> { Err(()) } +} + +impl<'a> Bar<'a> { + pub fn borrow<'b>(&'b self) -> Baz<'a, 'b> { Baz { bar: self } } + pub fn try_borrow<'b>(&'b self) -> Result<Baz<'a, 'b>, ()> { Ok(Baz { bar: self }) } + pub fn fail_borrow<'b>(&'b self) -> Result<Baz<'a, 'b>, ()> { Err(()) } +} + +impl<'a: 'b, 'b> Baz<'a, 'b> { + pub fn borrow<'c>(&'c self) -> Qux<'a, 'b, 'c> { Qux { baz: self } } + pub fn try_borrow<'c>(&'c self) -> Result<Qux<'a, 'b, 'c>, ()> { Ok(Qux { baz: self }) } + pub fn fail_borrow<'c>(&'c self) -> Result<Qux<'a, 'b, 'c>, ()> { Err(()) } +} + +impl<'a: 'b, 'b: 'c, 'c> Qux<'a, 'b, 'c> { + pub fn borrow<'d>(&'d self) -> Xyzzy<'a, 'b, 'c, 'd> { Xyzzy { qux: self } } + pub fn try_borrow<'d>(&'d self) -> Result<Xyzzy<'a, 'b, 'c, 'd>, ()> { Ok(Xyzzy { qux: self }) } + pub fn fail_borrow<'d>(&'d self) -> Result<Xyzzy<'a, 'b, 'c, 'd>, ()> { Err(()) } +} + + +rental! { + mod rentals { + use super::*; + + #[rental] + pub struct ComplexRent { + foo: Box<Foo>, + bar: Box<Bar<'foo>>, + baz: Box<Baz<'foo, 'bar>>, + qux: Box<Qux<'foo, 'bar, 'baz>>, + xyzzy: Xyzzy<'foo, 'bar, 'baz, 'qux>, + } + } +} + + +#[test] +fn new() { + let foo = Foo { i: 5 }; + let _ = rentals::ComplexRent::new( + Box::new(foo), + |foo| Box::new(foo.borrow()), + |bar, _| Box::new(bar.borrow()), + |baz, _, _| Box::new(baz.borrow()), + |qux, _, _, _| qux.borrow() + ); + + let foo = Foo { i: 5 }; + let cr = rentals::ComplexRent::try_new( + Box::new(foo), + |foo| foo.try_borrow().map(|bar| Box::new(bar)), + |bar, _| bar.try_borrow().map(|baz| Box::new(baz)), + |baz, _, _| baz.try_borrow().map(|qux| Box::new(qux)), + |qux, _, _, _| qux.try_borrow() + ); + assert!(cr.is_ok()); + + let foo = Foo { i: 5 }; + let cr = rentals::ComplexRent::try_new( + Box::new(foo), + |foo| foo.try_borrow().map(|bar| Box::new(bar)), + |bar, _| bar.try_borrow().map(|baz| Box::new(baz)), + |baz, _, _| baz.try_borrow().map(|qux| Box::new(qux)), + |qux, _, _, _| qux.fail_borrow() + ); + assert!(cr.is_err()); +} + + +#[test] +fn read() { + let foo = Foo { i: 5 }; + let cr = rentals::ComplexRent::new( + Box::new(foo), + |foo| Box::new(foo.borrow()), + |bar, _| Box::new(bar.borrow()), + |baz, _, _| Box::new(baz.borrow()), + |qux, _, _, _| qux.borrow() + ); + let i = cr.rent(|xyzzy| xyzzy.qux.baz.bar.foo.i); + assert_eq!(i, 5); + + let iref = cr.ref_rent(|xyzzy| &xyzzy.qux.baz.bar.foo.i); + assert_eq!(*iref, 5); +} diff --git a/third_party/rust/rental/tests/complex_mut.rs b/third_party/rust/rental/tests/complex_mut.rs new file mode 100644 index 0000000000..3ea44440ab --- /dev/null +++ b/third_party/rust/rental/tests/complex_mut.rs @@ -0,0 +1,136 @@ +#[macro_use] +extern crate rental; + + +pub struct Foo { + i: i32, +} + +pub struct Bar<'a> { + foo: &'a mut Foo, +} + +pub struct Baz<'a: 'b, 'b> { + bar: &'b mut Bar<'a> +} + +pub struct Qux<'a: 'b, 'b: 'c, 'c> { + baz: &'c mut Baz<'a, 'b> +} + +pub struct Xyzzy<'a: 'b, 'b: 'c, 'c: 'd, 'd> { + qux: &'d mut Qux<'a, 'b, 'c> +} + + +impl Foo { + pub fn borrow_mut<'a>(&'a mut self) -> Bar<'a> { Bar { foo: self } } + pub fn try_borrow_mut<'a>(&'a mut self) -> Result<Bar<'a>, ()> { Ok(Bar { foo: self }) } + pub fn fail_borrow_mut<'a>(&'a mut self) -> Result<Bar<'a>, ()> { Err(()) } +} + +impl<'a> Bar<'a> { + pub fn borrow_mut<'b>(&'b mut self) -> Baz<'a, 'b> { Baz { bar: self } } + pub fn try_borrow_mut<'b>(&'b mut self) -> Result<Baz<'a, 'b>, ()> { Ok(Baz { bar: self }) } + pub fn fail_borrow_mut<'b>(&'b mut self) -> Result<Baz<'a, 'b>, ()> { Err(()) } +} + +impl<'a: 'b, 'b> Baz<'a, 'b> { + pub fn borrow_mut<'c>(&'c mut self) -> Qux<'a, 'b, 'c> { Qux { baz: self } } + pub fn try_borrow_mut<'c>(&'c mut self) -> Result<Qux<'a, 'b, 'c>, ()> { Ok(Qux { baz: self }) } + pub fn fail_borrow_mut<'c>(&'c mut self) -> Result<Qux<'a, 'b, 'c>, ()> { Err(()) } +} + +impl<'a: 'b, 'b: 'c, 'c> Qux<'a, 'b, 'c> { + pub fn borrow_mut<'d>(&'d mut self) -> Xyzzy<'a, 'b, 'c, 'd> { Xyzzy { qux: self } } + pub fn try_borrow_mut<'d>(&'d mut self) -> Result<Xyzzy<'a, 'b, 'c, 'd>, ()> { Ok(Xyzzy { qux: self }) } + pub fn fail_borrow_mut<'d>(&'d mut self) -> Result<Xyzzy<'a, 'b, 'c, 'd>, ()> { Err(()) } +} + + +rental! { + mod rentals { + use super::*; + + #[rental_mut] + pub struct ComplexRent { + foo: Box<Foo>, + bar: Box<Bar<'foo>>, + baz: Box<Baz<'foo, 'bar>>, + qux: Box<Qux<'foo, 'bar, 'baz>>, + xyzzy: Xyzzy<'foo, 'bar, 'baz, 'qux>, + } + } +} + + +#[test] +fn new() { + let foo = Foo { i: 5 }; + let _ = rentals::ComplexRent::new( + Box::new(foo), + |foo| Box::new(foo.borrow_mut()), + |bar| Box::new(bar.borrow_mut()), + |baz| Box::new(baz.borrow_mut()), + |qux| qux.borrow_mut() + ); + + let foo = Foo { i: 5 }; + let cm = rentals::ComplexRent::try_new( + Box::new(foo), + |foo| foo.try_borrow_mut().map(|bar| Box::new(bar)), + |bar| bar.try_borrow_mut().map(|baz| Box::new(baz)), + |baz| baz.try_borrow_mut().map(|qux| Box::new(qux)), + |qux| qux.try_borrow_mut() + ); + assert!(cm.is_ok()); + + let foo = Foo { i: 5 }; + let cm = rentals::ComplexRent::try_new( + Box::new(foo), + |foo| foo.try_borrow_mut().map(|bar| Box::new(bar)), + |bar| bar.try_borrow_mut().map(|baz| Box::new(baz)), + |baz| baz.try_borrow_mut().map(|qux| Box::new(qux)), + |qux| qux.fail_borrow_mut() + ); + assert!(cm.is_err()); +} + + +#[test] +fn read() { + let foo = Foo { i: 5 }; + let cm = rentals::ComplexRent::new( + Box::new(foo), + |foo| Box::new(foo.borrow_mut()), + |bar| Box::new(bar.borrow_mut()), + |baz| Box::new(baz.borrow_mut()), + |qux| qux.borrow_mut() + ); + let i = cm.rent(|xyzzy| xyzzy.qux.baz.bar.foo.i); + assert_eq!(i, 5); + + let iref = cm.ref_rent(|xyzzy| &xyzzy.qux.baz.bar.foo.i); + assert_eq!(*iref, 5); +} + + +#[test] +fn write() { + let foo = Foo { i: 5 }; + let mut cm = rentals::ComplexRent::new( + Box::new(foo), + |foo| Box::new(foo.borrow_mut()), + |bar| Box::new(bar.borrow_mut()), + |baz| Box::new(baz.borrow_mut()), + |qux| qux.borrow_mut() + ); + + { + let iref: &mut i32 = cm.ref_rent_mut(|xyzzy| &mut xyzzy.qux.baz.bar.foo.i); + *iref = 12; + } + + let i = cm.rent(|xyzzy| xyzzy.qux.baz.bar.foo.i); + assert_eq!(i, 12); +} diff --git a/third_party/rust/rental/tests/covariant.rs b/third_party/rust/rental/tests/covariant.rs new file mode 100644 index 0000000000..545c2714ae --- /dev/null +++ b/third_party/rust/rental/tests/covariant.rs @@ -0,0 +1,52 @@ +#[macro_use] +extern crate rental; + + +//use std::marker::PhantomData; + + +pub struct Foo { + i: i32, +} + + +//pub struct Invariant<'a> { +// iref: &'a i32, +// inv: PhantomData<&'a mut &'a ()>, +//} + + +rental! { + mod rentals { + use super::*; + + #[rental(covariant)] + pub struct SimpleRef { + foo: Box<Foo>, + iref: &'foo i32, + } + + #[rental_mut(covariant)] + pub struct SimpleMut { + foo: Box<Foo>, + iref: &'foo mut i32, + } + +// #[rental(covariant)] +// pub struct ShouldBreak { +// foo: Box<Foo>, +// inv: Invariant<'foo>, +// } + } +} + + +#[test] +fn borrow() { + let foo = Foo { i: 5 }; + let fr = rentals::SimpleRef::new(Box::new(foo), |foo| &foo.i); + let b = fr.all(); + assert_eq!(**b.iref, 5); +} + + diff --git a/third_party/rust/rental/tests/debug.rs b/third_party/rust/rental/tests/debug.rs new file mode 100644 index 0000000000..59cf183b9b --- /dev/null +++ b/third_party/rust/rental/tests/debug.rs @@ -0,0 +1,39 @@ +#[macro_use] +extern crate rental; + + +#[derive(Debug)] +pub struct Foo { + i: i32, +} + + +rental! { + mod rentals { + use super::*; + + #[rental(debug, deref_suffix)] + pub struct SimpleRef { + foo: Box<Foo>, + iref: &'foo i32, + } + + #[rental_mut(debug, deref_suffix)] + pub struct SimpleMut { + foo: Box<Foo>, + iref: &'foo mut i32, + } + } +} + + +#[test] +fn print() { + let foo = Foo { i: 5 }; + let sr = rentals::SimpleRef::new(Box::new(foo), |foo| &foo.i); + println!("{:?}", sr); + + let foo = Foo { i: 5 }; + let sm = rentals::SimpleMut::new(Box::new(foo), |foo| &mut foo.i); + println!("{:?}", sm); +} diff --git a/third_party/rust/rental/tests/drop_order.rs b/third_party/rust/rental/tests/drop_order.rs new file mode 100644 index 0000000000..642438b8d2 --- /dev/null +++ b/third_party/rust/rental/tests/drop_order.rs @@ -0,0 +1,129 @@ +#![allow(dead_code)] + +#[macro_use] +extern crate rental; + +use std::cell::RefCell; +use std::rc::Rc; + + +pub struct Foo { + i: i32, + d: Rc<RefCell<String>>, +} + +pub struct Bar<'a> { + foo: &'a Foo, + d: Rc<RefCell<String>>, +} + +pub struct Baz<'a: 'b, 'b> { + bar: &'b Bar<'a>, + d: Rc<RefCell<String>>, +} + +pub struct Qux<'a: 'b, 'b: 'c, 'c> { + baz: &'c Baz<'a, 'b>, + d: Rc<RefCell<String>>, +} + +pub struct Xyzzy<'a: 'b, 'b: 'c, 'c: 'd, 'd> { + qux: &'d Qux<'a, 'b, 'c>, + d: Rc<RefCell<String>>, +} + + +impl Foo { + pub fn borrow<'a>(&'a self) -> Bar<'a> { Bar { foo: self, d: self.d.clone() } } +} + +impl Drop for Foo { + fn drop(&mut self) { + self.d.borrow_mut().push_str("Foo"); + } +} + +impl<'a> Bar<'a> { + pub fn borrow<'b>(&'b self) -> Baz<'a, 'b> { Baz { bar: self, d: self.d.clone() } } +} + +impl<'a> Drop for Bar<'a> { + fn drop(&mut self) { + self.d.borrow_mut().push_str("Bar"); + } +} + +impl<'a: 'b, 'b> Baz<'a, 'b> { + pub fn borrow<'c>(&'c self) -> Qux<'a, 'b, 'c> { Qux { baz: self, d: self.d.clone() } } +} + +impl<'a: 'b, 'b> Drop for Baz<'a, 'b> { + fn drop(&mut self) { + self.d.borrow_mut().push_str("Baz"); + } +} + +impl<'a: 'b, 'b: 'c, 'c> Qux<'a, 'b, 'c> { + pub fn borrow<'d>(&'d self) -> Xyzzy<'a, 'b, 'c, 'd> { Xyzzy { qux: self, d: self.d.clone() } } +} + +impl<'a: 'b, 'b: 'c, 'c> Drop for Qux<'a, 'b, 'c> { + fn drop(&mut self) { + self.d.borrow_mut().push_str("Qux"); + } +} + +impl<'a: 'b, 'b: 'c, 'c: 'd, 'd> Drop for Xyzzy<'a, 'b, 'c, 'd> { + fn drop(&mut self) { + self.d.borrow_mut().push_str("Xyzzy"); + } +} + + +rental! { + pub mod rentals { + use super::*; + + #[rental] + pub struct DropTestRent { + foo: Box<Foo>, + bar: Box<Bar<'foo>>, + baz: Box<Baz<'foo, 'bar>>, + qux: Box<Qux<'foo, 'bar, 'baz>>, + xyzzy: Xyzzy<'foo, 'bar, 'baz, 'qux>, + } + } +} + + +#[test] +fn drop_order() { + let d = Rc::new(RefCell::new(String::new())); + { + let foo = Foo { i: 5, d: d.clone() }; + let _ = rentals::DropTestRent::new( + Box::new(foo), + |foo| Box::new(foo.borrow()), + |bar, _| Box::new(bar.borrow()), + |baz, _, _| Box::new(baz.borrow()), + |qux, _, _, _| qux.borrow() + ); + } + assert_eq!(*d.borrow(), "XyzzyQuxBazBarFoo"); + + let d = Rc::new(RefCell::new(String::new())); + { + let foo = Foo { i: 5, d: d.clone() }; + let r = rentals::DropTestRent::new( + Box::new(foo), + |foo| Box::new(foo.borrow()), + |bar, _| Box::new(bar.borrow()), + |baz, _, _| Box::new(baz.borrow()), + |qux, _, _, _| qux.borrow() + ); + + let _head = r.into_head(); + } + assert_eq!(*d.borrow(), "XyzzyQuxBazBarFoo"); +} + diff --git a/third_party/rust/rental/tests/generic.rs b/third_party/rust/rental/tests/generic.rs new file mode 100644 index 0000000000..9043025d62 --- /dev/null +++ b/third_party/rust/rental/tests/generic.rs @@ -0,0 +1,53 @@ +#[macro_use] +extern crate rental; + + +pub struct Foo<T: 'static> { + t: T, +} + +impl<T: 'static> Foo<T> { + fn try_borrow(&self) -> Result<&T, ()> { Ok(&self.t) } + fn fail_borrow(&self) -> Result<&T, ()> { Err(()) } +} + + +rental! { + mod rentals { + type FooAlias<T> = super::Foo<T>; + + #[rental] + pub struct SimpleRef<T: 'static> { + foo: Box<FooAlias<T>>, + tref: &'foo T, + } + } +} + + +#[test] +fn new() { + let foo = Foo { t: 5 }; + let _ = rentals::SimpleRef::new(Box::new(foo), |foo| &foo.t); + + let foo = Foo { t: 5 }; + let sr = rentals::SimpleRef::try_new(Box::new(foo), |foo| foo.try_borrow()); + assert!(sr.is_ok()); + + let foo = Foo { t: 5 }; + let sr = rentals::SimpleRef::try_new(Box::new(foo), |foo| foo.fail_borrow()); + assert!(sr.is_err()); +} + + +#[test] +fn read() { + let foo = Foo { t: 5 }; + + let sr = rentals::SimpleRef::new(Box::new(foo), |foo| &foo.t); + let t: i32 = sr.rent(|tref| **tref); + assert_eq!(t, 5); + + let tref: &i32 = sr.ref_rent(|tref| *tref); + assert_eq!(*tref, 5); +} diff --git a/third_party/rust/rental/tests/lt_params.rs b/third_party/rust/rental/tests/lt_params.rs new file mode 100644 index 0000000000..3ce6d3eee7 --- /dev/null +++ b/third_party/rust/rental/tests/lt_params.rs @@ -0,0 +1,65 @@ +#[macro_use] +extern crate rental; + + +pub struct Foo<'a> { + i: &'a i32, +} + +impl<'a> Foo<'a> { + fn borrow(&self) -> &i32 { self.i } + fn try_borrow(&self) -> Result<&i32, ()> { Ok(self.i) } + fn fail_borrow(&self) -> Result<&i32, ()> { Err(()) } +} + + +rental! { + mod rentals { + use super::*; + + #[rental] + pub struct LtParam<'a> { + foo: Box<Foo<'a>>, + iref: &'foo i32, + } + } +} + + +#[test] +fn new() { + let i = 5; + + let foo = Foo { i: &i }; + let _ = rentals::LtParam::new(Box::new(foo), |foo| foo.borrow()); + + let foo = Foo { i: &i }; + let sr = rentals::LtParam::try_new(Box::new(foo), |foo| foo.try_borrow()); + assert!(sr.is_ok()); + + let foo = Foo { i: &i }; + let sr = rentals::LtParam::try_new(Box::new(foo), |foo| foo.fail_borrow()); + assert!(sr.is_err()); +} + + +#[test] +fn read() { + let i = 5; + + let foo = Foo { i: &i }; + + let mut sr = rentals::LtParam::new(Box::new(foo), |foo| foo.borrow()); + + { + let i: i32 = sr.rent(|iref| **iref); + assert_eq!(i, 5); + } + + { + let iref: &i32 = sr.ref_rent(|iref| *iref); + assert_eq!(*iref, 5); + } + + assert_eq!(sr.rent_all_mut(|borrows| *borrows.foo.i), 5); +} diff --git a/third_party/rust/rental/tests/map.rs b/third_party/rust/rental/tests/map.rs new file mode 100644 index 0000000000..3ef3b94810 --- /dev/null +++ b/third_party/rust/rental/tests/map.rs @@ -0,0 +1,42 @@ +#[macro_use] +extern crate rental; + + +pub struct Foo { + i: i32, +} + + +rental! { + mod rentals { + use super::*; + + #[rental(map_suffix = "T")] + pub struct SimpleRef<T: 'static> { + foo: Box<Foo>, + fr: &'foo T, + } + + #[rental_mut(map_suffix = "T")] + pub struct SimpleMut<T: 'static> { + foo: Box<Foo>, + fr: &'foo mut T, + } + } +} + + +#[test] +fn map() { + let foo = Foo { i: 5 }; + let sr = rentals::SimpleRef::new(Box::new(foo), |foo| foo); + let sr = sr.map(|fr| &fr.i); + assert_eq!(sr.rent(|ir| **ir), 5); + + let foo = Foo { i: 12 }; + let sm = rentals::SimpleMut::new(Box::new(foo), |foo| foo); + let sm = sm.map(|fr| &mut fr.i); + assert_eq!(sm.rent(|ir| **ir), 12); +} + + 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); + } +} diff --git a/third_party/rust/rental/tests/simple_ref.rs b/third_party/rust/rental/tests/simple_ref.rs new file mode 100644 index 0000000000..2cb7765578 --- /dev/null +++ b/third_party/rust/rental/tests/simple_ref.rs @@ -0,0 +1,103 @@ +#[macro_use] +extern crate rental; + + +pub struct Foo { + i: i32, +} + +impl Foo { + fn try_borrow(&self) -> Result<&i32, ()> { Ok(&self.i) } + fn fail_borrow(&self) -> Result<&i32, ()> { Err(()) } +} + +pub struct FooRef<'i> { + iref: &'i i32, + misc: i32, +} + + +impl<'i> ::std::ops::Deref for FooRef<'i> { + type Target = i32; + + fn deref(&self) -> &i32 { self.iref } +} + + +rental! { + mod rentals { + use super::*; + + #[rental] + pub struct SimpleRef { + foo: Box<Foo>, + fr: FooRef<'foo>, + } + } +} + + +#[test] +fn new() { + let foo = Foo { i: 5 }; + let _ = rentals::SimpleRef::new(Box::new(foo), |foo| FooRef{ iref: &foo.i, misc: 12 }); + + let foo = Foo { i: 5 }; + let sr: rental::RentalResult<rentals::SimpleRef, (), _> = rentals::SimpleRef::try_new(Box::new(foo), |foo| Ok(FooRef{ iref: foo.try_borrow()?, misc: 12 })); + assert!(sr.is_ok()); + + let foo = Foo { i: 5 }; + let sr: rental::RentalResult<rentals::SimpleRef, (), _> = rentals::SimpleRef::try_new(Box::new(foo), |foo| Ok(FooRef{ iref: foo.fail_borrow()?, misc: 12 })); + assert!(sr.is_err()); +} + + +#[test] +fn read() { + let foo = Foo { i: 5 }; + + let mut sr = rentals::SimpleRef::new(Box::new(foo), |foo| FooRef{ iref: &foo.i, misc: 12 }); + + { + let i: i32 = sr.rent(|iref| **iref); + assert_eq!(i, 5); + } + + { + let iref: &i32 = sr.ref_rent(|fr| fr.iref); + assert_eq!(*iref, 5); + let iref: Option<&i32> = sr.maybe_ref_rent(|fr| Some(fr.iref)); + assert_eq!(iref, Some(&5)); + let iref: Result<&i32, ()> = sr.try_ref_rent(|fr| Ok(fr.iref)); + assert_eq!(iref, Ok(&5)); + } + + { + assert_eq!(sr.head().i, 5); + assert_eq!(sr.rent_all(|borrows| borrows.foo.i), 5); + assert_eq!(sr.rent_all_mut(|borrows| borrows.foo.i), 5); + } + + { + let iref: Option<&i32> = sr.maybe_ref_rent_all(|borrows| Some(borrows.fr.iref)); + assert_eq!(iref, Some(&5)); + let iref: Result<&i32, ()> = sr.try_ref_rent_all(|borrows| Ok(borrows.fr.iref)); + assert_eq!(iref, Ok(&5)); + } + + { + let iref: &mut i32 = sr.ref_rent_all_mut(|borrows| &mut borrows.fr.misc); + *iref = 57; + assert_eq!(*iref, 57); + } + + { + let iref: Option<&mut i32> = sr.maybe_ref_rent_all_mut(|borrows| Some(&mut borrows.fr.misc)); + assert_eq!(iref, Some(&mut 57)); + } + + { + let iref: Result<&mut i32, ()> = sr.try_ref_rent_all_mut(|borrows| Ok(&mut borrows.fr.misc)); + assert_eq!(iref, Ok(&mut 57)); + } +} diff --git a/third_party/rust/rental/tests/string.rs b/third_party/rust/rental/tests/string.rs new file mode 100644 index 0000000000..9f6c7ea9da --- /dev/null +++ b/third_party/rust/rental/tests/string.rs @@ -0,0 +1,30 @@ +#[macro_use] +extern crate rental; + + +rental! { + pub mod rent_string { + #[rental(deref_suffix)] + pub struct OwnedStr { + buffer: String, + slice: &'buffer str, + slice_2: &'slice str, + } + } +} + + +#[test] +fn new() { + let buf = "Hello, World!".to_string(); + let _ = rent_string::OwnedStr::new(buf, |slice| slice, |slice, _| slice); +} + + +#[test] +fn read() { + let buf = "Hello, World!".to_string(); + let rbuf = rent_string::OwnedStr::new(buf, |slice| slice, |slice, _| slice); + + assert_eq!(&*rbuf, "Hello, World!"); +} diff --git a/third_party/rust/rental/tests/subrental.rs b/third_party/rust/rental/tests/subrental.rs new file mode 100644 index 0000000000..f44a072aee --- /dev/null +++ b/third_party/rust/rental/tests/subrental.rs @@ -0,0 +1,88 @@ +#[macro_use] +extern crate rental; + + +pub struct Foo { + pub i: i32, +} + +pub struct Bar<'a> { + pub foo: &'a Foo, +} + +pub struct Qux<'a: 'b, 'b> { + pub bar: &'b Bar<'a>, +} + + +impl Foo { + pub fn borrow(&self) -> Bar { Bar { foo: self } } + pub fn try_borrow<'a>(&'a self) -> Result<Bar<'a>, ()> { Ok(Bar { foo: self }) } + pub fn fail_borrow<'a>(&'a self) -> Result<Bar<'a>, ()> { Err(()) } +} + +impl<'a> Bar<'a> { + pub fn borrow<'b>(&'b self) -> Qux<'a, 'b> { Qux { bar: self } } + pub fn try_borrow<'b>(&'b self) -> Result<Qux<'a, 'b>, ()> { Ok(Qux { bar: self }) } + pub fn fail_borrow<'b>(&'b self) -> Result<Qux<'a, 'b>, ()> { Err(()) } +} + + +rental! { + pub mod rentals { + use super::*; + + #[rental] + pub struct Sub { + foo: Box<Foo>, + bar: Bar<'foo>, + } + + #[rental] + pub struct Rent { + #[subrental = 2] + sub: Box<Sub>, + qux: Qux<'sub_0, 'sub_1>, + } + + #[rental] + pub struct BorrowSub<'f> { + foo: &'f Foo, + bar: Bar<'foo>, + } + + #[rental] + pub struct TailRent { + foo: Box<Foo>, + #[subrental = 2] + sub: Box<BorrowSub<'foo>>, + iref: &'sub_1 i32, + } + } +} + + +#[test] +fn new() { + let foo = Foo { i: 5 }; + let sub = rentals::Sub::new(Box::new(foo), |foo| foo.borrow()); + let _ = rentals::Rent::new(Box::new(sub), |sub| sub.bar.borrow()); + + let foo = Foo { i: 5 }; + let sub = rentals::Sub::new(Box::new(foo), |foo| foo.borrow()); + let rent = rentals::Rent::try_new(Box::new(sub), |sub| sub.bar.try_borrow()); + assert!(rent.is_ok()); + + let foo = Foo { i: 5 }; + let sub = rentals::Sub::new(Box::new(foo), |foo| foo.borrow()); + let rent = rentals::Rent::try_new(Box::new(sub), |sub| sub.bar.fail_borrow()); + assert!(rent.is_err()); +} + + +#[test] +fn read() { + let foo = Foo { i: 5 }; + let sub = rentals::Sub::new(Box::new(foo), |foo| foo.borrow()); + let _ = rentals::Rent::new(Box::new(sub), |sub| sub.bar.borrow()); +} diff --git a/third_party/rust/rental/tests/target_ty_hack.rs b/third_party/rust/rental/tests/target_ty_hack.rs new file mode 100644 index 0000000000..d98bd0d7b5 --- /dev/null +++ b/third_party/rust/rental/tests/target_ty_hack.rs @@ -0,0 +1,54 @@ +#[macro_use] +extern crate rental; + + +type MyVec<T> = Vec<T>; + + +rental! { + pub mod rent_vec_slice { + use super::*; + + #[rental] + pub struct OwnedSlice { + #[target_ty = "[u8]"] + buffer: MyVec<u8>, + slice: &'buffer [u8], + } + + #[rental_mut] + pub struct OwnedMutSlice { + #[target_ty = "[u8]"] + buffer: MyVec<u8>, + slice: &'buffer mut [u8], + } + } +} + + +#[test] +fn new() { + let vec = vec![1, 2, 3]; + let _ = rent_vec_slice::OwnedSlice::new(vec, |slice| slice); +} + + +#[test] +fn read() { + let vec = vec![1, 2, 3]; + let rvec = rent_vec_slice::OwnedSlice::new(vec, |slice| slice); + + assert_eq!(rvec.rent(|slice| slice[1]), 2); + assert_eq!(rvec.rent(|slice| slice[1]), rvec.rent(|slice| slice[1])); +} + + +#[test] +fn write() { + let vec = vec![1, 2, 3]; + let mut rvec = rent_vec_slice::OwnedMutSlice::new(vec, |slice| slice); + + rvec.rent_mut(|slice| slice[1] = 4); + assert_eq!(rvec.rent(|slice| slice[1]), 4); + assert_eq!(rvec.rent(|slice| slice[1]), rvec.rent(|slice| slice[1])); +} diff --git a/third_party/rust/rental/tests/trait.rs b/third_party/rust/rental/tests/trait.rs new file mode 100644 index 0000000000..180e328ce1 --- /dev/null +++ b/third_party/rust/rental/tests/trait.rs @@ -0,0 +1,33 @@ +#[macro_use] +extern crate rental; + + +pub trait MyTrait { } + + +pub struct MyStruct { } + + +impl MyTrait for MyStruct { } + + +rental! { + pub mod rentals { + use ::MyTrait; + + #[rental] + pub struct RentTrait { + my_trait: Box<MyTrait + 'static>, + my_suffix: &'my_trait (MyTrait + 'static), + } + } +} + + +#[test] +fn new() { + let _tr = rentals::RentTrait::new( + Box::new(MyStruct{}), + |t| &*t, + ); +} diff --git a/third_party/rust/rental/tests/unused.rs b/third_party/rust/rental/tests/unused.rs new file mode 100644 index 0000000000..a8962a050c --- /dev/null +++ b/third_party/rust/rental/tests/unused.rs @@ -0,0 +1,28 @@ +use std::rc::Rc; + +#[macro_use] +extern crate rental; + +pub struct Sample { + field: i32, +} + +rental! { + mod sample_rental { + use super::*; + + #[rental] + pub struct SampleRental { + sample: Rc<Sample>, + sref: &'sample i32, + } + } +} +use self::sample_rental::SampleRental; + +#[test] +fn unused() { + let sample = Rc::new(Sample { field: 42 }); + let rental = SampleRental::new(sample, |sample_rc| &sample_rc.field); + rental.rent(|this| println!("{}", this)); +} diff --git a/third_party/rust/rental/tests/vec_slice.rs b/third_party/rust/rental/tests/vec_slice.rs new file mode 100644 index 0000000000..afc46edf7c --- /dev/null +++ b/third_party/rust/rental/tests/vec_slice.rs @@ -0,0 +1,47 @@ +#[macro_use] +extern crate rental; + + +rental! { + pub mod rent_vec_slice { + #[rental] + pub struct OwnedSlice { + buffer: Vec<u8>, + slice: &'buffer [u8], + } + + #[rental_mut] + pub struct OwnedMutSlice { + buffer: Vec<u8>, + slice: &'buffer mut [u8], + } + } +} + + +#[test] +fn new() { + let vec = vec![1, 2, 3]; + let _ = rent_vec_slice::OwnedSlice::new(vec, |slice| slice); +} + + +#[test] +fn read() { + let vec = vec![1, 2, 3]; + let rvec = rent_vec_slice::OwnedSlice::new(vec, |slice| slice); + + assert_eq!(rvec.rent(|slice| slice[1]), 2); + assert_eq!(rvec.rent(|slice| slice[1]), rvec.rent(|slice| slice[1])); +} + + +#[test] +fn write() { + let vec = vec![1, 2, 3]; + let mut rvec = rent_vec_slice::OwnedMutSlice::new(vec, |slice| slice); + + rvec.rent_mut(|slice| slice[1] = 4); + assert_eq!(rvec.rent(|slice| slice[1]), 4); + assert_eq!(rvec.rent(|slice| slice[1]), rvec.rent(|slice| slice[1])); +} |