Skip to content

Instantly share code, notes, and snippets.

@soarez
Created April 23, 2022 09:11
Show Gist options
  • Save soarez/491cb9945fcd953b7ac147a25a3749d8 to your computer and use it in GitHub Desktop.
Save soarez/491cb9945fcd953b7ac147a25a3749d8 to your computer and use it in GitHub Desktop.
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