traits

How to get Deref coercion when using impl Trait

我与影子孤独终老i 提交于 2019-12-24 10:23:37
问题 This function returns the first element of a list-like collection. It works for a variety of different list-like types: fn first<T: Copy>(x: impl Deref<Target=[T]>) -> T { x[0] } For example, this compiles and runs: let data: Vec<usize> = vec![3, 4]; assert_eq!(first(data), 3); let data: &[usize] = &[3, 4]; assert_eq!(first(data), 3); let data: Rc<[usize]> = Rc::new([3, 4]); assert_eq!(first(data), 3); This also compiles and runs: fn stub(x: &[usize]) -> usize { first(x) } let data: &[usize;

What are the possible operators for traits in a where clause in Rust?

。_饼干妹妹 提交于 2019-12-24 09:28:19
问题 I'm learning Rust and got to the chapter on trait bounds. In that chapter, they give an example with the + (plus) operator to enumerate all required traits in a where clause. What other operators are allowed on traits in Rust? I searched but I wasn't able to find any documentation about this. Does Rust support 'or' notation, brackets, negation? 回答1: 'or' notation No. Such a thing doesn't make sense to me — what would the code do if something could be A OR B ? brackets No, unless you count the

What happens during template initialization?

折月煮酒 提交于 2019-12-24 09:28:18
问题 In Scala official docs, it says that: If this is the template of a trait then its mixin-evaluation consists of an evaluation of the statement sequence statsstats. If this is not a template of a trait, then its evaluation consists of the following steps. First, the superclass constructor sc is evaluated. Then, all base classes in the template's linearization up to the template's superclass denoted by sc are mixin-evaluated. Mixin-evaluation happens in reverse order of occurrence in the

Why does the compiler not infer the concrete type of an associated type of an impl trait return value?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-24 08:25:55
问题 I have a trait with an associated type: pub trait Speak { type Error; fn speak(&self) -> Result<String, Self::Error>; } An implementation of that trait: #[derive(Default)] pub struct Dog; impl Speak for Dog { type Error = (); fn speak(&self) -> Result<String, Self::Error> { Ok("woof".to_string()) } } And a function returning an instance of that implementation: pub fn speaker() -> impl Speak { Dog::default() } I know that in this example I could just use Dog as the return type, but in my

Can I define a trait whose implementations must be `!Send`?

送分小仙女□ 提交于 2019-12-24 08:25:44
问题 I'd like to define a trait which forces its implementors to under no circumstances be sent to, or shared between, threads. It should suffice to mark the trait as !Send , but Rust doesn't seem to let me. Is it possible? Example (playground): #![feature(optin_builtin_traits)] // This is a syntax error //trait ThreadThing : !Send {} // This doesn't work either trait ThreadThing { } impl !Send for ThreadThing {} 回答1: No, you can't make !Send a condition of ThreadThing . The compiler just doesn't

How to implement PartialEq on Vector for my own structs?

喜欢而已 提交于 2019-12-24 08:25:39
问题 I have the following definition: pub struct List<T> { memory: Vec<T>, } I would get the equivalent of #[derive(PartialEq)] for this type like describe in How can I implement PartialEq? I use a match expression, like: impl<T: PartialEq> PartialEq for List<T> { fn eq(&self, other: &List<T>) -> bool { self.memory == other.memory } } impl<T: fmt::Debug> fmt::Debug for List<T> where T:Display { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { try!(write!(f, "[")); for (count, v) in self

Use Trait Function with Same Name but Optionally

和自甴很熟 提交于 2019-12-24 07:45:09
问题 PHP Class Using Same Name as Trait Function Refer to the question I just asked above here. Here was my original code. trait sampletrait{ function hello(){ echo "hello from trait"; } } class client{ use sampletrait; function hello(){ echo "hello from class"; //From within here, how do I call traits hello() function also? } } I can call the trait function like this thanks to the answer to the question. class client{ use sampletrait { hello as protected sampletrait_hello; } function hello(){

laravel 4 trait autoloading in models

半腔热情 提交于 2019-12-24 07:07:08
问题 OK, I am struggling for over 2 hours now... It must be one of the "oh god, it was that obvious" times where you are so tired and you cannot see the solution... I want to extend the eloquentmodel to include a trait. so my class looks like <?php class Note extends Eloquent{ use \Admin\GreatTrait; ... } and I created the app/Traits/Admin directory structure and there I created the file named GreatTrait.php with the following contents: <?php namespace Admin; trait GreatTrait{ ... } and of cource

Scala: overriding type member with bounds

爱⌒轻易说出口 提交于 2019-12-24 03:25:51
问题 I've narrowed down my issue to the following minimal (non-)working example: class Z trait A[E <: Z] { type T[X <: E] <: A[X] } trait B[E <: Z] extends A[E] { type T[X <: E] <: B[X] } trait C[E <: Z, F[X <: Z] <: C[X, F]] extends A[E] { type T[X <: E] = F[X] } class D[E <: Z] extends B[E] with C[E, D] It is sort of a higher-kinded F-bounded polymorphism (I'm using T[E] as the return value of some methods). When I try to compile this code I get: error: overriding type T in trait B with bounds[X

Can we automatically derive a user-defined trait? [duplicate]

不打扰是莪最后的温柔 提交于 2019-12-24 02:37:09
问题 This question already has answers here : Is it possible to add your own derivable traits, or are these fixed by the compiler? (2 answers) Closed 2 years ago . I have a struct like this #[derive(CustomTrait)] struct Sample { v: Vec<u8>, } and my trait goes like this trait CustomTrait {...} Can I do the above stuff? It threw an error for me. I want something similar to the Clone trait. Is this possible with Rust? 回答1: #[derive(Foo, Bar)] is sugar for #[derive_Foo] #[derive_Bar] , so it is