Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- trait Material {
- fn bar(&self) {
- println!("mat!");
- }
- }
- struct Lambertian {}
- impl Material for Lambertian{
- fn bar(&self) {
- println!("lambertian!");
- }
- }
- trait Mesh {
- fn foo(&self) {
- println!("mesh!");
- }
- }
- struct Sphere<'a> {
- mat: &'a Material,
- }
- impl<'a> Sphere<'a> {
- fn new<T>(material: &T) -> Sphere
- where
- T: Material + 'a,
- {
- return Sphere {
- mat: material,
- };
- }
- }
- impl<'a> Mesh for Sphere<'a> {
- fn foo(&self) {
- println!("sphere!");
- self.mat.bar();
- }
- }
- pub fn main() {
- let l = Lambertian {};
- let meshes: Vec<Box<dyn Mesh>> = vec![Box::new(Sphere::new(&l))];
- meshes[0].foo();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement