Skip to content

Instantly share code, notes, and snippets.

@jimblandy
Created April 15, 2015 17:37
Show Gist options
  • Save jimblandy/0deb0555a5c5761ffa5d to your computer and use it in GitHub Desktop.
Save jimblandy/0deb0555a5c5761ffa5d to your computer and use it in GitHub Desktop.
Implicit Deref
mod implicit_deref {
use std::ops::Deref;
struct T(i32);
struct S<'a>(&'a T);
impl<'a> Deref for S<'a> {
type Target = T;
fn deref(&self) -> &T { &self.0; }
}
fn f(t: T, n: i32) { assert_eq!(n, t.0); }
#[test]
fn test() {
let t = T(42);
let s = S(&t);
f(s, 42);
}
}
@jimblandy
Copy link
Author

Dies with:

cd ~/rust/play2 && cargo test
   Compiling play2 v0.0.1 (file:///home/jimb/rust/play2)
src/main.rs:193:11: 193:12 error: mismatched types:
 expected `implicit_deref::T`,
    found `implicit_deref::S<'_>`
(expected struct `implicit_deref::T`,
    found struct `implicit_deref::S`) [E0308]
src/main.rs:193         f(s, 42);
                          ^
error: aborting due to previous error
Could not compile `play2`.

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