traits

Generic implementation depending on traits

北城余情 提交于 2020-12-23 11:50:26
问题 When defining a generic struct , is there a way in Rust to use different implementation of a method according to which trait is implemented by the given generic type T ? For example: struct S<T> { value: T, } impl<T> S<T> { fn print_me(&self) { println!("I cannot be printed"); } } impl<T: std::fmt::Display> S<T> { fn print_me(&self) { println!("{}", self.value); } } fn main() { let s = S { value: 2 }; s.print_me(); } 回答1: There is an unstable feature known as specialization which permits

Rust trait issues trait cannot be made into an object

拥有回忆 提交于 2020-12-15 04:32:06
问题 I'm familiar with rust but i suck with traits so forgive me for my ignorance. So Im trying to write some code that will generate a random struct with a random value. have the following trait and helper macros for the structs use rand::{thread_rng, Rng}; use std::fmt; pub trait DataType { /// generate a new instance of the type with a random value fn random() -> Box<Self>; /// generate a new instance of the same type with a random value fn gen_another(&self) -> Box<Self>; } macro_rules! impl

Rust trait issues trait cannot be made into an object

纵然是瞬间 提交于 2020-12-15 04:31:50
问题 I'm familiar with rust but i suck with traits so forgive me for my ignorance. So Im trying to write some code that will generate a random struct with a random value. have the following trait and helper macros for the structs use rand::{thread_rng, Rng}; use std::fmt; pub trait DataType { /// generate a new instance of the type with a random value fn random() -> Box<Self>; /// generate a new instance of the same type with a random value fn gen_another(&self) -> Box<Self>; } macro_rules! impl

How to use dynamic dispatch with a method which takes an iterator as a parameter?

荒凉一梦 提交于 2020-12-06 12:21:00
问题 I am writing a command line application in rust for processing audio from a sensor. I would like the user to be able to choose an algorithm or filter to apply from several options. I was hoping to use dynamic dispatch to switch out a struct which implements my filter trait at runtime. However, this is not allowed by the compiler, because one of the trait methods takes a generic parameter. How could I implement this same functionality, without causing any compiler troubles? I know that an easy

How to use dynamic dispatch with a method which takes an iterator as a parameter?

倖福魔咒の 提交于 2020-12-06 12:20:49
问题 I am writing a command line application in rust for processing audio from a sensor. I would like the user to be able to choose an algorithm or filter to apply from several options. I was hoping to use dynamic dispatch to switch out a struct which implements my filter trait at runtime. However, this is not allowed by the compiler, because one of the trait methods takes a generic parameter. How could I implement this same functionality, without causing any compiler troubles? I know that an easy

Struct with a generic trait which is also a generic trait

↘锁芯ラ 提交于 2020-11-29 08:51:48
问题 In Rust 1.15, I have created a trait to abstract over reading & parsing file format(s). I'm trying to create a struct which has this generic trait inside. I have this trait: use std::io::Read; trait MyReader<R: Read> { fn new(R) -> Self; fn into_inner(self) -> R; fn get_next(&mut self) -> Option<u32>; fn do_thingie(&mut self); } I want to make a struct which has a reference to something that implements this. struct MyIterThing<'a, T: MyReader<R>+'a> { inner: &'a mut T, } Gives the following