Created
April 23, 2022 09:11
-
-
Save soarez/491cb9945fcd953b7ac147a25a3749d8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::ops::Deref; | |
pub struct Foo; | |
impl Foo { | |
pub fn f(&self) -> &str { "Foo" } | |
} | |
pub struct BoxA<T>(T); | |
impl<T> Deref for BoxA<T> { | |
type Target = T; | |
fn deref(&self) -> &Self::Target { &self.0 } | |
} | |
impl<T> BoxA<T> { | |
pub fn f(&self) -> &str { "BoxA" } | |
} | |
pub struct BoxB<T>(T); | |
impl<T> Deref for BoxB<T> { | |
type Target = T; | |
fn deref(&self) -> &Self::Target { &self.0 } | |
} | |
impl<T> BoxB<T> { | |
pub fn f(&self) -> &str { "BoxB" } | |
} | |
pub struct BoxC<T>(T); | |
impl<T> Deref for BoxC<T> { | |
type Target = T; | |
fn deref(&self) -> &Self::Target { &self.0 } | |
} | |
#[cfg(test)] | |
mod tests { | |
use super::*; | |
#[test] | |
fn deref() { | |
let v = BoxB(BoxA(Foo)); | |
assert_eq!(v.f(), "BoxB"); | |
assert_eq!((*v).f(), "BoxA"); | |
assert_eq!((*(*v)).f(), "Foo"); | |
let u = BoxC(BoxA(Foo)); | |
assert_eq!(u.f(), "BoxA"); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment