traits

Is there some way to implement a trait on multiple traits?

£可爱£侵袭症+ 提交于 2019-12-11 02:37:12
问题 Why doesn't this work: trait Update { fn update(&mut self); } trait A {} trait B {} impl<T: A> Update for T { fn update(&mut self) { println!("A") } } impl<U: B> Update for U { fn update(&mut self) { println!("B") } } error[E0119]: conflicting implementations of trait `Update`: --> src/main.rs:14:1 | 8 | impl<T: A> Update for T { | ----------------------- first implementation here ... 14 | impl<U: B> Update for U { | ^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation I would assume it is

How to use associated constants to define the length of an array? [duplicate]

左心房为你撑大大i 提交于 2019-12-11 02:18:58
问题 This question already has answers here : Can associated constants be used to initialize the length of fixed size arrays? (2 answers) Closed last year . I've got a trait which represents an entity that can be sent through UDP sockets: pub trait ToNetEnt { const NET_SIZE: usize; fn from_net(data: &[u8]) -> Self; fn to_net(&self) -> &[u8]; } Though there is an associated constant NET_SIZE , I couldn't use it in methods: pub fn req<T: ToNetEnt>(&self) -> T { let buf: [u8; T::NET_SIZE]; } because

What is the cited problem with using generic type parameters in trait objects?

岁酱吖の 提交于 2019-12-11 02:18:46
问题 I am reading Object Safety Is Required for Trait Objects and I don't understand the problem with generic type parameters. The same is true of generic type parameters that are filled in with concrete type parameters when the trait is used: the concrete types become part of the type that implements the trait. When the type is forgotten through the use of a trait object, there is no way to know what types to fill in the generic type parameters with. I am trying to code an example but I can't

Template argument deduction/substitution failed, when using typename argument

北慕城南 提交于 2019-12-11 01:56:34
问题 I have the following code, which defines a template struct W that exports a type T that's based on the template argument to W : #include <iostream> using namespace std; template <unsigned N> struct Y {}; template <unsigned N> struct W { using T = Y<N>; }; I then defined this template function that looks at this type T : template <unsigned N> void foo (const typename W<N>::T& in) { //-- } The problem here is that if I try calling this function from main using one of the types exported as T ,

Scala: How to inherit the same trait twice?

耗尽温柔 提交于 2019-12-11 01:54:06
问题 I'm following along in Odersky's "Programming in Scala" 2nd edition, and in section 12.5 "Traits as stackable modifications", he presents an IntQueue along with a trait that doubles any values you insert into the queue: import scala.collection.mutable.ArrayBuffer abstract class IntQueue { def get(): Int def put(x: Int) } class BasicIntQueue extends IntQueue { private val buf = new ArrayBuffer[Int] def get() = buf.remove(0) def put(x: Int) { buf += x } } trait Doubling extends IntQueue {

Scala: class type required but T found

橙三吉。 提交于 2019-12-11 01:25:59
问题 I've found similar issues of this particular problem, however the problem was due to someone trying to instantiate T directly. Here I'm trying to create a trait that is a general interface to extend classes and store them automatically in a database such as Riak using classOf[T] . Using Scala 2.10. Here's my code: trait RiakWriteable[T] { /** * bucket name of data in Riak holding class data */ def bucketName: String /** * determine whether secondary indices will be added */ def enable2i:

How do I define trait bounds on an associated type?

大兔子大兔子 提交于 2019-12-11 00:58:59
问题 I want to write a function that accepts Iterator of type that has ToString trait. What I have in mind: fn parse<T: Iterator /* ?T::Item : ToString? */>(mut args: T) -> Result<String, String> { match args.next() { Some(x) => x.to_string(), None => String::from("Missing parameter"), } } 回答1: Yes, you can do that with a where clause: fn parse<T: Iterator>(mut args: T) -> Result<String, String> where <T as Iterator>::Item: ToString, { // .... } Or, since it's unambiguous which Item is meant here,

How can I mass implement Deserialize for all types that implement a specific trait?

折月煮酒 提交于 2019-12-10 22:24:25
问题 I am deserializing a YAML config file with Serde. For most structs I deserialize into, things are quite simple — there's a one-to-one relationship between the fields of the structs and the properties in my YAML file. In a few cases, things are a bit more complicated. For these, the properties in the YAML file are better viewed as parameters to the constructor. The actual struct will have different fields, calculated from those. For these cases, I have written separate config structs that I

“Subclassing” traits in Rust

这一生的挚爱 提交于 2019-12-10 21:59:20
问题 I have a situation where several of my structs should implement multiple traits, but all of them implement at least one trait in common. When I get hold of a mixed bag of these structs, I want to treat them all as being of the common trait: pass them as method parameters typed to that trait, store them in collections typed for that trait, etc. I haven't been able to figure out how to do it. Here is some code where I try to do the way it was suggested here, but it fails to compile: trait

Scala: Example use for early definition / early initializer / pre-initialized fields

与世无争的帅哥 提交于 2019-12-10 20:44:53
问题 Scala allows you to make early definitions like so: trait A { val v: Int } class B extends { val v = 4 } with A What is an example use of this feature? 回答1: Whenever the value is used for the trait initialization. So for eaxmple for this trait: trait UsefulTrait { val parameter : Int private val myHelperObject = new MyExpensiveClass(parameter) } The parameter is used to substitute for a constructor parameter. However the parameter should be rather made an abstract method, because it leaves