summaryrefslogtreecommitdiffstats
path: root/third_party/rust/rental/tests/subrental.rs
diff options
context:
space:
mode:
Diffstat (limited to 'third_party/rust/rental/tests/subrental.rs')
-rw-r--r--third_party/rust/rental/tests/subrental.rs88
1 files changed, 88 insertions, 0 deletions
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());
+}