SHARE
TWEET

Untitled

a guest Sep 19th, 2019 89 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. pub trait Engine {
  2.     fn add(a: usize, b: usize) -> usize;
  3.    
  4.     #[cfg(feature = "gpu")]
  5.     fn gpu_add(a: usize, b: usize) -> usize {
  6.         // Fallback implementation
  7.         Self::add(a, b)
  8.     }
  9. }
  10. pub struct MyEngine {}
  11.  
  12. impl Engine for MyEngine {
  13.     fn add(a: usize, b: usize) -> usize {
  14.         a + b
  15.     }
  16.    
  17.     #[cfg(feature = "gpu")]
  18.     fn gpu_add(a: usize, b: usize) -> usize {
  19.         println!("fast gpu add");
  20.         a + b
  21.     }
  22. }
  23.  
  24. pub struct OtherEngine {}
  25.  
  26. impl Engine for OtherEngine {
  27.     fn add(a: usize, b: usize) -> usize {
  28.         a + b
  29.     }
  30.    
  31.     // Uses fallback implementation in the gpu case
  32. }
  33.  
  34. pub fn use_engine<T: Engine>(engine: T) {
  35.     #[cfg(feature = "gpu")]
  36.     let res = T::gpu_add(1, 1);
  37.    
  38.     #[cfg(not(feature = "gpu"))]
  39.     let res = T::add(1, 1);
  40. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top