Trouble with trait bound function and 'static

I am having trouble using a function that takes a trait bound as a parameter. I have implemented a trait for another trait.

The function is define like


fn update_last_read_time<T: UniqueNamespace>(point: T) {

    let unique_id = point.get_unique_namespace(true).expect("get_unique_namespace failed");
}

but I am getting the error

update_last_read_time(point);
   |     --------------------- ^^^^^ the trait `UniqueNamespace` is not implemented for `Box<TestPoint>`
   |     |
   |     required by a bound introduced by this call
   |
   = help: the trait `UniqueNamespace` is implemented for `(dyn Point + 'static)`
note: required by a bound in `update_last_read_time`

I thought I could pass a Box of a dynamic trait and it would work ?
I am not sure where 'static fits in which is my issue I think.

Test case is at

ErrorMsg: the trait UniqueNamespace is not implemented for Box<TestPoint>

It tells the solution:

-impl UniqueNamespace for dyn Point {
+impl UniqueNamespace for Box<dyn Point> {

-let point = Box::new(TestPoint{});
+let point = Box::new(TestPoint {}) as Box<dyn Point>;

Thanks I was under the impression that was automatic somehow.

Many traits have implementations for Box<T> when T implements the trait. But that is something you would have to do explicitly when you define a trait of your own.

2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.