While using boxed closures I ran into the following issue:
type Test = Rc<dyn Fn() -> i64>;
fn test_bad() -> Test {
Test::new(|| 42)
}
fn test_good() -> Test {
Rc::new(|| 42)
}
In the first case, I'm using the type alias to refer to the new
method, whereas I'm using Rc
directly in the second case.
In the first case, the compiler complains:
| Test::new(|| 42)
| ^^^ function or associated item not found in `Rc<(dyn Fn() -> i64 + 'static)>`
|
= note: the method `new` exists but the following trait bounds were not satisfied:
`dyn Fn() -> i64: Sized`
But the second case works just fine. Can somebody please explain the difference? Is there any way to refer to new
through the type alias or do I need to wrap it myself?