Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. trait Material {
  2. fn bar(&self) {
  3. println!("mat!");
  4. }
  5. }
  6. struct Lambertian {}
  7. impl Material for Lambertian{
  8. fn bar(&self) {
  9. println!("lambertian!");
  10. }
  11. }
  12.  
  13. trait Mesh {
  14. fn foo(&self) {
  15. println!("mesh!");
  16. }
  17. }
  18. struct Sphere<'a> {
  19. mat: &'a Material,
  20. }
  21. impl<'a> Sphere<'a> {
  22. fn new<T>(material: &T) -> Sphere
  23. where
  24. T: Material + 'a,
  25. {
  26. return Sphere {
  27. mat: material,
  28. };
  29. }
  30. }
  31. impl<'a> Mesh for Sphere<'a> {
  32. fn foo(&self) {
  33. println!("sphere!");
  34. self.mat.bar();
  35. }
  36. }
  37.  
  38. pub fn main() {
  39. let l = Lambertian {};
  40.  
  41. let meshes: Vec<Box<dyn Mesh>> = vec![Box::new(Sphere::new(&l))];
  42.  
  43. meshes[0].foo();
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement