Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A real-world example #9

Open
sdroege opened this issue Sep 19, 2017 · 0 comments
Open

A real-world example #9

sdroege opened this issue Sep 19, 2017 · 0 comments

Comments

@sdroege
Copy link

sdroege commented Sep 19, 2017

In the documentation you're looking for a real-world example, and here I have one. Condensed it looks like this (and you guess right if you think this is actually related to FFI under the hood).

I have a struct

struct Foo {
  imp: Box<FooImp>,
}

and a trait

trait FooImp: mopa::Any {
    // things in here
}

The use case here is that Foo is a generic thing with exchangeable implementation, but I want it to be with dynamic dispatch and not generic for reasons (if needed I can explain).

Now all fine so far, but Foo is actually always passed around as an Arc<Foo>, and it's possible to connect "notifications" to Foo

impl Foo {
    fn connect_notification<F: Fn(&Foo)>(&self, func: F) {
        ...
    }
}

Now a FooImp implementation might want to connect to some notification. But it wants to get access to itself again somehow. Here we need mopa:

impl Foo {
    fn get_imp(&self) -> &FooImp { ... }
}
let self: &SomeFooImpl;
let foo: &Foo;

foo.connect_notification(|foo| {
    let ourself = foo.get_imp().downcast_ref::<SomeFooImpl>().unwrap();
    ...
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant