I have a construct that looks like this:
struct Manager {
the_fn: Box<dyn Fn(u32)>
}
impl Manager {
pub fn new<T: 'static>(user_fn: impl Fn(u32, &T) + 'static, data: T) -> Self {
Self {
the_fn: Box::new(move |param| {
user_fn(param, &data)
})
}
}
}
Can be used like:
fn test() {
Manager::new(my_func, String::from("users.rust-lang.org"));
}
fn my_func(param: u32, data: &String) {
/* ... */
}
But I would like to allow for this signature:
fn my_func(param: u32, data: &str) {
/* ... */
}
So what I tried is this, by using the AsRef<T>
trait:
struct Manager {
the_fn: Box<dyn Fn(u32)>
}
impl Manager {
pub fn new<R: 'static, T: AsRef<R> + 'static>(user_fn: impl Fn(u32, &R) + 'static, data: T) -> Self {
Self {
the_fn: Box::new(move |param| {
user_fn(param, AsRef::as_ref(&data))
})
}
}
}
However, I get error:
the size for values of type
str
cannot be known at compilation time
the traitSized
is not implemented forstr
I understand that size of str
cannot be known at compilation time.
But we only ever use it as a reference/borrow!
So, is there any way how this code could be fixed to work as intended?
...or do I need a completely different approach?
Thanks.