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::Hash; #[delegatable_trait] pub trait Map { type K; type V; } #[delegatable_trait] pub trait Get: Map { fn get(&self, k: &Q) -> Option<&Self::V>; //~ ERROR function cannot return without recursing [unconditional_recursion] } impl Map for HashMap { type K = K; type V = V; } impl Map for BTreeMap { type K = K; type V = V; } #[delegate_remote] #[delegate(Get, target = "self", generics = "X", where = "K: Hash + Eq + Borrow, X: Hash + Eq + ?Sized")] //Forgot S: BuildHasher struct HashMap(); #[delegate_remote] #[delegate(Get, target = "self", generics = "X", where = "K: Ord + Borrow, X: Ord + ?Sized")] struct BTreeMap(); #[derive(Delegate)] #[delegate(Map)] #[delegate(Get, generics = "X", where = "X: ?Sized, A: Map, B: Map")] 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")); }