I'm trying to wrap my head around trait objects and how I can use them. One scenario is I might want to pass a function for a callback, when some condition is met that callback is called.
fn bind_callback( key: u64, /* pass function */) {
// when key is matched with the event, call the function
}
How can I do this though? I hear I can use trait objects for something like this, but how would I go about implementing this? Can someone show me an example? Here's what I'm at:
trait Callback {
fn callback(self);
}
fn pass_callback(f: &Callback) {
f.callback();
}
fn run_me() {
println!("Hello World!");
}
fn main() {
pass_callback(&run_me); // run simple no arg void ret function
pass_callback(|| println!("Hello World!")); // same thing
}
I know this is terribly wrong, I'm trying to understand how I would accomplish something like this. My error output is:
<anon>:14:19: 14:26 error: the trait `Callback` is not implemented for the type `fn() {run_me}` [E0277]
<anon>:14 pass_callback(&run_me);
^~~~~~~
<anon>:14:19: 14:26 help: see the detailed explanation for E0277
<anon>:14:19: 14:26 note: required for the cast to the object type `Callback`
<anon>:15:19: 15:46 error: mismatched types:
expected `&Callback`,
found `[closure@<anon>:15:19: 15:46]`
(expected &-ptr,
found closure) [E0308]
<anon>:15 pass_callback(|| println!("Hello World!"));
^~~~~~~~~~~~~~~~~~~~~~~~~~~
<anon>:15:19: 15:46 help: see the detailed explanation for E0308
error: aborting due to 2 previous errors
playpen: application terminated with error code 101
If you want to use closures and functions as parameters, you cannot use your own traits. Instead, you use one of the Fn*
family:
fn pass_callback<F>(f: F)
where F: Fn()
{
f();
}
fn run_me() {
println!("Hello World!");
}
fn main() {
pass_callback(run_me);
pass_callback(|| println!("Hello World!"));
}
Fn
FnOnce
FnMut
If you really want to use your own trait, you can, but then you need to implement the trait on something and pass that item into your function:
trait Callback {
fn callback(&self, value: u8) -> bool;
}
struct IsEven;
impl Callback for IsEven {
fn callback(&self, value: u8) -> bool {
value % 2 == 0
}
}
fn pass_callback<C>(f: C)
where C: Callback
{
if f.callback(42) {
println!("Callback passed");
}
}
fn main() {
pass_callback(IsEven);
}
As an aside, your comment
no arg void ret function
isn't quite true. Rust doesn't have a void
"type", it has the empty tuple, often called the unit type.
The reason your code doesn't work is that the function run_me
and closure || println!("Hello World!")
don't have implementations for your callback trait. You can fix this by adding a Callback
implementation for all types implementing the Fn()
trait:
trait Callback {
fn callback(&self);
}
fn pass_callback(f: &Callback) {
f.callback();
}
fn run_me() {
println!("Hello World!");
}
// For every type T that implements Fn(),
// this is the Callback implementation.
impl<T: Fn()> Callback for T {
fn callback(&self) {
self()
}
}
fn main() {
pass_callback(&run_me); // a simple function
pass_callback(&|| println!("Hello World!")); // a closure
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With