0

I'm using a trait to define an associated type.

pub trait SharedType{ type Item; } 

There are cases where I need to implement this trait on a reference &T. However, in these situations I'd like to auto-implement SharedType on a wrapper struct

pub struct  NoRef< T > { inner: T }

The problem seems to be extracting an associated type while avoiding the use of lifetime parameters. One thing I've tried is

impl < T > SharedType for NoRef< T >
    where for <'a> 
        &'a T: SharedType { 
    type Item = <&'static T as SharedType>::Item; 
}

The syntax for <'a> works fine for enforcing the requirement &'a T: SharedType, however something else seems to be needed in order to complete the line type Item = ... Here's the error message

error[E0310]: the parameter type `T` may not live long enough
  --> src/matrices/matrix_types/oracle_deref.rs:31:17
   |
31 |     type Item = <&'static T as SharedType>::Item; 
   |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ...so that the reference type `&'static T` does not outlive the data it points at
   |
help: consider adding an explicit lifetime bound...
   |
28 | impl < T: 'static > SharedType for NoRef< T >
   |         +++++++++
GHPR
  • 45
  • 6

0 Answers0