Skip to main content
updated to use dyn syntax
Source Link
Peter Hall
  • 59.7k
  • 17
  • 168
  • 229

You can either add a type parameter to your struct, as in Zernike's answer, or use a trait object.

Using the type parameter is better for performance because each value of T will create a specialized copy of the struct, which allows for static dispatch. A trait object uses dynamic dispatch so it lets you swap the concrete type at runtime.

The trait object approach looks like this:

pub struct MyStruct<'a> { my_trait: &'a dyn MyTrait, } 

Or this:

pub struct MyStruct { my_trait: Box<MyTrait>Box<dyn MyTrait>, } 

However, in your case, MyStruct cannot be made into an object because receive is a static method. You'd need to change it to take &self or &mut self as its first argument for this to work. There are also other restrictions.

You can either add a type parameter to your struct, as in Zernike's answer, or use a trait object.

Using the type parameter is better for performance because each value of T will create a specialized copy of the struct, which allows for static dispatch. A trait object uses dynamic dispatch so it lets you swap the concrete type at runtime.

The trait object approach looks like this:

pub struct MyStruct<'a> { my_trait: &'a MyTrait, } 

Or this:

pub struct MyStruct { my_trait: Box<MyTrait>, } 

However, in your case, MyStruct cannot be made into an object because receive is a static method. You'd need to change it to take &self or &mut self as its first argument for this to work. There are also other restrictions.

You can either add a type parameter to your struct, as in Zernike's answer, or use a trait object.

Using the type parameter is better for performance because each value of T will create a specialized copy of the struct, which allows for static dispatch. A trait object uses dynamic dispatch so it lets you swap the concrete type at runtime.

The trait object approach looks like this:

pub struct MyStruct<'a> { my_trait: &'a dyn MyTrait, } 

Or this:

pub struct MyStruct { my_trait: Box<dyn MyTrait>, } 

However, in your case, MyStruct cannot be made into an object because receive is a static method. You'd need to change it to take &self or &mut self as its first argument for this to work. There are also other restrictions.

added 43 characters in body
Source Link
Shepmaster
  • 439.3k
  • 116
  • 1.3k
  • 1.5k

You can either add a type parameter to your struct, as in Zernike's answerZernike's answer, or use a trait object.

Using the type parameter is better for performance because each value of T will create a specialized copy of the struct, which allows for static dispatch. But aA trait object uses dynamic dispatch so it lets you swap the concrete type at runtime.

The trait object approach looks like this:

pub struct MyStruct<'a> { myTraitmy_trait: &'a MyTrait, } 

Or this:

pub struct MyStruct { myTraitmy_trait: Box<MyTrait>, } 

However, in your case, MyStruct cannot be made into an object because the receive is a static method. You'd need to change it to take &self or &mut self as its first argument for this to work. There are also other restrictions.

You can either add a type parameter to your struct, as in Zernike's answer, or use a trait object.

Using the type parameter is better for performance because each value of T will create a specialized copy of the struct, which allows for static dispatch. But a trait object uses dynamic dispatch so it lets you swap the concrete type at runtime.

The trait object approach looks like this:

pub struct MyStruct<'a> { myTrait: &'a MyTrait } 

Or this:

pub struct MyStruct { myTrait: Box<MyTrait> } 

However, in your case, MyStruct cannot be made into an object because the receive is a static method. You'd need to change it to take &self or &mut self as its first argument for this to work. There are also other restrictions.

You can either add a type parameter to your struct, as in Zernike's answer, or use a trait object.

Using the type parameter is better for performance because each value of T will create a specialized copy of the struct, which allows for static dispatch. A trait object uses dynamic dispatch so it lets you swap the concrete type at runtime.

The trait object approach looks like this:

pub struct MyStruct<'a> { my_trait: &'a MyTrait, } 

Or this:

pub struct MyStruct { my_trait: Box<MyTrait>, } 

However, in your case, MyStruct cannot be made into an object because receive is a static method. You'd need to change it to take &self or &mut self as its first argument for this to work. There are also other restrictions.

deleted 8 characters in body
Source Link
Peter Hall
  • 59.7k
  • 17
  • 168
  • 229

You can either add a type parameter to your struct, as in Zernike's answer, or use a trait object.

Using the type parameter is better for performance because each value of T will create a specialized copy of the struct, which allows for static dispatch. But a trait object uses dynamic dispatch so it lets you swap the concrete type at runtime.

The trait object approach looks like this:

pub struct MyStruct<'a> { myTrait: &'a MyTrait } 

Or this:

pub struct MyStruct { myTrait: Box<MyTrait> } 

However, in your case, MyStruct cannot be made into an object because the receive is a static method. You'd need to change it to take self, &self or &mut self as its first argument for this to work. There are also other restrictions.

You can either add a type parameter to your struct, as in Zernike's answer, or use a trait object.

Using the type parameter is better for performance because each value of T will create a specialized copy of the struct, which allows for static dispatch. But a trait object uses dynamic dispatch so it lets you swap the concrete type at runtime.

The trait object approach looks like this:

pub struct MyStruct<'a> { myTrait: &'a MyTrait } 

Or this:

pub struct MyStruct { myTrait: Box<MyTrait> } 

However, in your case, MyStruct cannot be made into an object because the receive is a static method. You'd need to change it to take self, &self or &mut self as its first argument for this to work. There are also other restrictions.

You can either add a type parameter to your struct, as in Zernike's answer, or use a trait object.

Using the type parameter is better for performance because each value of T will create a specialized copy of the struct, which allows for static dispatch. But a trait object uses dynamic dispatch so it lets you swap the concrete type at runtime.

The trait object approach looks like this:

pub struct MyStruct<'a> { myTrait: &'a MyTrait } 

Or this:

pub struct MyStruct { myTrait: Box<MyTrait> } 

However, in your case, MyStruct cannot be made into an object because the receive is a static method. You'd need to change it to take &self or &mut self as its first argument for this to work. There are also other restrictions.

added 302 characters in body
Source Link
Peter Hall
  • 59.7k
  • 17
  • 168
  • 229
Loading
Source Link
Peter Hall
  • 59.7k
  • 17
  • 168
  • 229
Loading