Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #![allow(unused)]
- extern crate either; // 1.5.1
- use either::Either;
- use std::fs;
- trait Common {
- fn bounded_box(self) -> f32;
- }
- trait B: Common {
- // B specific stuff here
- }
- trait C: Common {
- // C specific stuff here
- }
- impl Common for f64 {
- fn bounded_box(self) -> f32 {
- return 2.0;
- }
- }
- impl B for f64 {}
- impl Common for f32 {
- fn bounded_box(self) -> f32 {
- return 1.0;
- }
- }
- impl C for f32 {}
- fn interact<T: B, U: C>(x: Either<T, U>) -> f32 {
- match x {
- Either::Left(v) => v.bounded_box(),
- Either::Right(v) => v.bounded_box(),
- }
- }
- // Wait! We can just impl Common for Either!
- impl<T, U> Common for Either<T, U>
- where T: Common,
- U: Common {
- fn bounded_box(self) -> f32 {
- match self {
- Either::Left(v) => v.bounded_box(),
- Either::Right(v) => v.bounded_box(),
- }
- }
- }
- fn main() {
- println!("{}", Either::<f64, f32>::Right(1.0f32).bounded_box());
- println!("{}", Either::<f64, f32>::Left(1.0f64).bounded_box());
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement