-
Notifications
You must be signed in to change notification settings - Fork 13
/
delegate_remote.rs
56 lines (44 loc) · 1.05 KB
/
delegate_remote.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
extern crate ambassador;
use ambassador::{delegatable_trait, delegate_remote};
#[delegatable_trait]
pub trait Shout {
fn shout(&self, input: &str) -> String;
}
pub struct Cat;
impl Shout for Cat {
fn shout(&self, input: &str) -> String {
format!("{} - meow!", input)
}
}
pub struct Dog;
impl Shout for Dog {
fn shout(&self, input: &str) -> String {
format!("{} - wuff!", input)
}
}
mod wrapped {
use super::{Cat};
pub struct WrappedAnimals<A> {
pub foo: Cat,
pub bar: A,
baz: u32, // private field
}
impl<A> WrappedAnimals<A> {
pub fn new(foo: Cat, bar: A) -> Self {
WrappedAnimals{foo, bar, baz: 13}
}
}
}
use wrapped::*;
#[delegate_remote]
#[delegate(Shout, target = "bar")]
struct WrappedAnimals<A: Shout> {
foo: Cat,
bar: A,
}
pub fn main() {
let foo_animal = WrappedAnimals::new(Cat, Cat);
println!("{}", foo_animal.shout("BAR"));
let bar_animal = WrappedAnimals::new(Cat, Dog);
println!("{}", bar_animal.shout("BAR"));
}