Skip to content

Instantly share code, notes, and snippets.

@jdm
Created April 8, 2014 19:31
Show Gist options
  • Save jdm/10176997 to your computer and use it in GitHub Desktop.
Save jdm/10176997 to your computer and use it in GitHub Desktop.
pub trait OptionalRootable {
fn root<'a>(&self, roots: &'a RootCollection) -> Option<Root<'a>>;
}
impl<T> OptionalRootable for Option<T> {
fn root<'a>(&self, roots: &'a RootCollection) -> Option<Root<'a>> {
None
}
}
struct RootCollection;
impl RootCollection {
pub fn new() -> RootCollection {
RootCollection
}
}
pub struct Root<'a> {
root_list: &'a RootCollection,
ptr: uint,
}
impl<'a> Root<'a> {
pub fn root_ref<'b>(&'b self) -> JSRef<'b> {
unsafe {
JSRef {
ptr: self.ptr,
chain: ::std::cast::transmute_region(&()),
}
}
}
}
pub struct JSRef<'a> {
priv ptr: uint,
chain: &'a (),
}
impl<'a> Eq for JSRef<'a> {
fn eq(&self, other: &JSRef) -> bool {
self.ptr == other.ptr
}
}
struct Document;
impl Document {
pub fn SetBody(&self, new_body: Option<JSRef>) {
let roots = RootCollection::new();
let old_body: Option<uint> = None;
let old_body = old_body.root(&roots);
old_body.as_ref().map(|inner| inner.root_ref()) == new_body;
}
}
fn main() {
}
@jdm
Copy link
Author

jdm commented Apr 8, 2014

root.rs:53:38: 53:44 error: `roots` does not live long enough
root.rs:53         let old_body = old_body.root(&roots);
                                                ^~~~~~
root.rs:49:52: 55:6 note: reference must be valid for the anonymous lifetime #2 defined on the block at 49:51...
root.rs:49     pub fn SetBody(&self, new_body: Option<JSRef>) {
root.rs:50         let roots = RootCollection::new();
root.rs:51 
root.rs:52         let old_body: Option<uint> = None;
root.rs:53         let old_body = old_body.root(&roots);
root.rs:54         old_body.as_ref().map(|inner| inner.root_ref()) == new_body;
           ...
root.rs:49:52: 55:6 note: ...but borrowed value is only valid for the block at 49:51
root.rs:49     pub fn SetBody(&self, new_body: Option<JSRef>) {
root.rs:50         let roots = RootCollection::new();
root.rs:51 
root.rs:52         let old_body: Option<uint> = None;
root.rs:53         let old_body = old_body.root(&roots);
root.rs:54         old_body.as_ref().map(|inner| inner.root_ref()) == new_body;
           ...
root.rs:54:9: 54:17 error: `old_body` does not live long enough
root.rs:54         old_body.as_ref().map(|inner| inner.root_ref()) == new_body;
                   ^~~~~~~~
root.rs:49:52: 55:6 note: reference must be valid for the anonymous lifetime #2 defined on the block at 49:51...
root.rs:49     pub fn SetBody(&self, new_body: Option<JSRef>) {
root.rs:50         let roots = RootCollection::new();
root.rs:51 
root.rs:52         let old_body: Option<uint> = None;
root.rs:53         let old_body = old_body.root(&roots);
root.rs:54         old_body.as_ref().map(|inner| inner.root_ref()) == new_body;
           ...
root.rs:49:52: 55:6 note: ...but borrowed value is only valid for the block at 49:51
root.rs:49     pub fn SetBody(&self, new_body: Option<JSRef>) {
root.rs:50         let roots = RootCollection::new();
root.rs:51 
root.rs:52         let old_body: Option<uint> = None;
root.rs:53         let old_body = old_body.root(&roots);
root.rs:54         old_body.as_ref().map(|inner| inner.root_ref()) == new_body;
           ...
error: aborting due to 2 previous errors

@alan-andrade
Copy link

impl<T> OptionalRootable for Option<T> {
    fn root<'a>(&self, roots: &'a RootCollection) -> Option<Root> {
        None
    }
}

returns

gistfile1.rs:54:9: 54:17 error: `old_body` does not live long enough
gistfile1.rs:54         old_body.as_ref().map(|inner| inner.root_ref()) == new_body;
                        ^~~~~~~~
gistfile1.rs:49:52: 55:6 note: reference must be valid for the anonymous lifetime #2 defined on the block at 49:51...
gistfile1.rs:49     pub fn SetBody(&self, new_body: Option<JSRef>) {
gistfile1.rs:50         let roots = RootCollection::new();
gistfile1.rs:51 
gistfile1.rs:52         let old_body: Option<uint> = None;
gistfile1.rs:53         let old_body = old_body.root(&roots);
gistfile1.rs:54         old_body.as_ref().map(|inner| inner.root_ref()) == new_body;
                ...
gistfile1.rs:49:52: 55:6 note: ...but borrowed value is only valid for the block at 49:51
gistfile1.rs:49     pub fn SetBody(&self, new_body: Option<JSRef>) {
gistfile1.rs:50         let roots = RootCollection::new();
gistfile1.rs:51 
gistfile1.rs:52         let old_body: Option<uint> = None;
gistfile1.rs:53         let old_body = old_body.root(&roots);
gistfile1.rs:54         old_body.as_ref().map(|inner| inner.root_ref()) == new_body;
                ...
error: aborting due to previous error

@alan-andrade
Copy link

LOC: 25

 pub fn root_ref<'b>(&'b self) -> JSRef<'a> {

outputs:

gistfile1.rs:2:35: 2:49 warning: private type in exported type signature, #[warn(visible_private_types)] on by default
gistfile1.rs:2     fn root<'a>(&self, roots: &'a RootCollection) -> Option<Root<'a>>;
                                                 ^~~~~~~~~~~~~~
gistfile1.rs:6:24: 6:29 warning: unused variable: `roots`, #[warn(unused_variable)] on by default
gistfile1.rs:6     fn root<'a>(&self, roots: &'a RootCollection) -> Option<Root> {
                                      ^~~~~
gistfile1.rs:14:5: 16:6 warning: code is never used: `new`, #[warn(dead_code)] on by default
gistfile1.rs:14     pub fn new() -> RootCollection {
gistfile1.rs:15         RootCollection
gistfile1.rs:16     }
gistfile1.rs:20:20: 20:34 warning: private type in exported type signature, #[warn(visible_private_types)] on by default
gistfile1.rs:20     root_list: &'a RootCollection,
                                   ^~~~~~~~~~~~~~
gistfile1.rs:46:1: 46:17 warning: code is never used: `Document`, #[warn(dead_code)] on by default
gistfile1.rs:46 struct Document;
                ^~~~~~~~~~~~~~~~
gistfile1.rs:49:5: 55:6 warning: code is never used: `SetBody`, #[warn(dead_code)] on by default
gistfile1.rs:49     pub fn SetBody(&self, new_body: Option<JSRef>) {
gistfile1.rs:50         let roots = &RootCollection::new();
gistfile1.rs:51 
gistfile1.rs:52         let old_body: Option<uint> = None;
gistfile1.rs:53         let old_body = old_body.root(roots);
gistfile1.rs:54         old_body.as_ref().map(|inner| inner.root_ref()) == new_body;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment