Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- pub trait Engine {
- fn add(a: usize, b: usize) -> usize;
- }
- pub trait GpuEngine: Engine {
- fn gpu_add(a: usize, b: usize) -> usize {
- // Fallback implementation
- Self::add(a, b)
- }
- }
- pub struct MyEngine {}
- impl Engine for MyEngine {
- fn add(a: usize, b: usize) -> usize {
- a + b
- }
- }
- impl GpuEngine for MyEngine {
- fn gpu_add(a: usize, b: usize) -> usize {
- println!("fast gpu add");
- a + b
- }
- }
- pub struct OtherEngine {}
- impl Engine for OtherEngine {
- fn add(a: usize, b: usize) -> usize {
- a + b
- }
- }
- // Uses fallback implementation
- impl GpuEngine for OtherEngine {}
- #[cfg(not(eature = "gpu-engine"))]
- pub fn use_engine<T: Engine>(engine: T) {
- // do sth cool with that engine
- let res = T::add(1, 1);
- }
- #[cfg(feature = "gpu-engine")]
- pub fn use_engine<T: GpuEngine>(engine: T) {
- // do sth cool with that gpu engine
- let res = T::gpu_add(1, 1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement