Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. pub trait Engine {
  2. fn add(a: usize, b: usize) -> usize;
  3. }
  4.  
  5. pub trait GpuEngine: Engine {
  6. fn gpu_add(a: usize, b: usize) -> usize {
  7. // Fallback implementation
  8. Self::add(a, b)
  9. }
  10. }
  11.  
  12. pub struct MyEngine {}
  13. impl Engine for MyEngine {
  14. fn add(a: usize, b: usize) -> usize {
  15. a + b
  16. }
  17. }
  18.  
  19. impl GpuEngine for MyEngine {
  20. fn gpu_add(a: usize, b: usize) -> usize {
  21. println!("fast gpu add");
  22. a + b
  23. }
  24. }
  25.  
  26. pub struct OtherEngine {}
  27.  
  28. impl Engine for OtherEngine {
  29. fn add(a: usize, b: usize) -> usize {
  30. a + b
  31. }
  32. }
  33.  
  34. // Uses fallback implementation
  35. impl GpuEngine for OtherEngine {}
  36.  
  37. #[cfg(not(eature = "gpu-engine"))]
  38. pub fn use_engine<T: Engine>(engine: T) {
  39. // do sth cool with that engine
  40. let res = T::add(1, 1);
  41. }
  42.  
  43. #[cfg(feature = "gpu-engine")]
  44. pub fn use_engine<T: GpuEngine>(engine: T) {
  45. // do sth cool with that gpu engine
  46. let res = T::gpu_add(1, 1);
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement