Skip to content

Instantly share code, notes, and snippets.

@DaGenix
Created November 17, 2013 02:17
Show Gist options
  • Save DaGenix/7508248 to your computer and use it in GitHub Desktop.
Save DaGenix/7508248 to your computer and use it in GitHub Desktop.
Lifetime thing
trait GetBuffer<'self> {
fn get(&self) -> &'self [u8];
}
// This works
struct Buff1<'self> {
b: &'self [u8]
}
impl <'self> GetBuffer<'self> for Buff1<'self> {
fn get(&self) -> &'self [u8] { self.b }
}
// This doesn't
struct Buff2 {
b: ~[u8]
}
impl <'self> GetBuffer<'self> for Buff2 {
fn get(&self) -> &'self [u8] { self.b.slice(0, 1) }
}
fn main() {}
@DaGenix
Copy link
Author

DaGenix commented Nov 17, 2013

Error:
testrp.rs:18:32: 18:52 error: cannot infer an appropriate lifetime due to conflicting requirements
testrp.rs:18 fn get(&self) -> &'self [u8] { self.b.slice(0, 1) }
^~~~~~~~~~~~~~~~~~~~
testrp.rs:18:32: 18:38 note: first, the lifetime cannot outlive the expression at 18:32...
testrp.rs:18 fn get(&self) -> &'self [u8] { self.b.slice(0, 1) }
^~~~~~
testrp.rs:18:32: 18:38 note: ...due to the following expression
testrp.rs:18 fn get(&self) -> &'self [u8] { self.b.slice(0, 1) }
^~~~~~
testrp.rs:18:32: 18:52 note: but, the lifetime must be valid for the method call at 18:32...
testrp.rs:18 fn get(&self) -> &'self [u8] { self.b.slice(0, 1) }
^~~~~~~~~~~~~~~~~~~~
testrp.rs:18:32: 18:38 note: ...due to the following expression
testrp.rs:18 fn get(&self) -> &'self [u8] { self.b.slice(0, 1) }
^~~~~~
error: aborting due to previous error
task 'rustc' failed at 'explicit failure', /home/enix/code/rust/src/libsyntax/diagnostic.rs:101
task '

' failed at 'explicit failure', /home/enix/code/rust/src/librustc/lib.rs:397

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