extern crate ambassador; use ambassador::{delegatable_trait, delegate_remote, Delegate}; use std::borrow::Borrow; use std::cmp::{Eq, Ord}; use std::collections::{BTreeMap, HashMap}; use std::hash::{BuildHasher, Hash}; #[delegatable_trait] pub trait Map { type K; type V; } #[delegatable_trait] pub trait Get: Map { fn get(&self, k: &Q) -> Option<&Self::V>; } impl Map for HashMap { type K = K; type V = V; } impl Map for BTreeMap { type K = K; type V = V; } // No automatic where clause provided for target = "self" #[delegate_remote] #[delegate(Get, generics = "X", target = "self", where = "K: Hash + Eq + Borrow, S: BuildHasher, X: Hash + Eq + ?Sized")] struct HashMap(); #[delegate_remote] #[delegate(Get, generics = "X", target = "self", where = "K: Ord + Borrow, X: Ord + ?Sized")] struct BTreeMap(); #[derive(Delegate)] #[delegate(Map)] #[delegate(Get, generics = "X", where = "X: ?Sized, A: Map, B: Map")] //auto where clause misses required on super trait pub enum Either { Left(A), Right(B), } pub fn main() { let my_map: Either, BTreeMap<&'static str, u32>> = Either::Left([("a", 1)].into()); println!("{:?}", my_map.get("a")); }