Last active
May 5, 2018 05:28
-
-
Save ryochack/075789b637e6df489691da68114fbf3d to your computer and use it in GitHub Desktop.
This file contains 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::marker::PhantomData; | |
use std::mem; | |
// Unit-like struct | |
struct U; | |
// Empty tuple struct | |
struct T(); | |
// Empty struct | |
struct E {} | |
// Empty tuple member struct | |
struct B { | |
_blank: (), | |
} | |
// PhantomData member struct | |
struct P { | |
_marker: PhantomData<*const ()>, | |
} | |
// Zero sized array member struct | |
struct A { | |
_arr: [u8; 0], | |
} | |
impl U { | |
fn size() -> usize { | |
mem::size_of::<Self>() | |
} | |
fn instance_method(&self) -> &'static str { | |
"instance_called" | |
} | |
} | |
impl T { | |
fn size() -> usize { | |
mem::size_of::<Self>() | |
} | |
fn instance_method(&self) -> &'static str { | |
"instance_called" | |
} | |
} | |
impl E { | |
fn size() -> usize { | |
mem::size_of::<Self>() | |
} | |
fn instance_method(&self) -> &'static str { | |
"instance_called" | |
} | |
} | |
impl B { | |
fn size() -> usize { | |
mem::size_of::<Self>() | |
} | |
fn instance_method(&self) -> &'static str { | |
"instance_called" | |
} | |
} | |
impl P { | |
fn size() -> usize { | |
mem::size_of::<Self>() | |
} | |
fn instance_method(&self) -> &'static str { | |
"instance_called" | |
} | |
} | |
impl A { | |
fn size() -> usize { | |
mem::size_of::<Self>() | |
} | |
fn instance_method(&self) -> &'static str { | |
"instance_called" | |
} | |
} | |
fn main() { | |
let u: U = U; | |
let t: T = T(); | |
let e: E = E {}; | |
let b: B = B { _blank: () }; | |
let p: P = P { | |
_marker: PhantomData, | |
}; | |
let a: A = A { _arr: [] }; | |
println!("U: size={}, {}", U::size(), u.instance_method()); | |
println!("T: size={}, {}", T::size(), t.instance_method()); | |
println!("E: size={}, {}", E::size(), e.instance_method()); | |
println!("B: size={}, {}", B::size(), b.instance_method()); | |
println!("P: size={}, {}", P::size(), p.instance_method()); | |
println!("A: size={}, {}", A::size(), a.instance_method()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment