traits

Implement IntoIterator for binary tree

喜你入骨 提交于 2019-12-02 12:24:10
I am trying to build a binary tree and write an iterator to traverse values in the tree. When implementing the IntoIterator trait for my tree nodes I ran into a problem with lifetimes src\main.rs:43:6: 43:8 error: the lifetime parameter `'a` is not constrained by the impl trait, self type, or predicates [E0207] src\main.rs:43 impl<'a, T: 'a> IntoIterator for Node<T> { I understand that I need to specify that NodeIterator will live as long as Node but I am unsure of how to express that use std::cmp::PartialOrd; use std::boxed::Box; struct Node<T: PartialOrd> { value: T, left: Option<Box<Node<T>

How to initialize a trait variable that is a val

梦想与她 提交于 2019-12-02 11:19:10
I have MyObject and MyTrait: class MyObject(private val myname: String = "") extends MyTrait { _name = myname def foo(myname : String) { _name = myname } } trait MyTrait { protected var _name: String = _ def name = _name } This works fine as this val myObject = new MyObject("abc") println(myObject.name) myObject.foo("def") println(myObject.name) prints abc def as expected. Problem now is that I want MyTrait._name to be a val instead of a var. But there is no way I can manage to get this to compile. Any hints appreciated. Regards, Oliver Here is an answer that uses the very latest cutting-edge

Why is it necessary to add redundant trait bounds even though my trait uses those same traits as bounds?

廉价感情. 提交于 2019-12-02 11:05:15
问题 I have been trying to code a trait which requires a type to implement Add (and further down the line other operations for vector spaces) with itself as well as among its references. The following is a small example, illustrating the problem I ran into: use std::ops::Add; #[derive(Debug)] struct MyVec<T>(Vec<T>); impl<'a, 'b, T: Copy + Add> Add<&'a MyVec<T>> for &'b MyVec<T> { type Output = MyVec<T::Output>; fn add(self, other: &'a MyVec<T>) -> Self::Output { /* ... */ } } impl<'a, T: Copy +

Is it possible to have a generic function on a trait?

試著忘記壹切 提交于 2019-12-02 09:58:31
I have: struct Plumbus<'a> { grumbo: &'a Grumbo, } trait Grumbo { fn dinglebop<T>(&self, x: &mut T) -> bool { false } } but I get: error[E0038]: the trait `Grumbo` cannot be made into an object --> plumbus.rs:4:5 | 4 | grumbo: &'a Grumbo, | ^^^^^^^^^^^^^^^^^^ the trait `Grumbo` cannot be made into an object | = note: method `dinglebop` has generic type parameters I want to have dinglebop do nothing by default, but depending on the Grumbo and the T , possibly fill x with a T if it makes sense for that particular Grumbo implementation. In C++ this could probably be accomplished with partial

Curiously recurring generic trait pattern: overflow evaluating the requirement

蓝咒 提交于 2019-12-02 08:37:32
I am trying to implement a generic structure with a bunch of fields, where each of the field types should know about the exact type of the whole structure. It's a sort of strategy pattern. pub struct Example<S: Strategy<Example<S, D>>, D> { pub s: S, pub a: S::Associated, pub data: D, } pub trait Strategy<T> { type Associated; fn run(&self, &T); } pub trait HasData { type Data; fn data(&self) -> &Self::Data; } impl<S: Strategy<Self>, D> Example<S, D> { // ^^^^ // the complex code in this impl is the actual meat of the library: pub fn do_it(&self) { self.s.run(self); // using the Strategy trait

Specifying generic parameter to belong to a small set of types

不问归期 提交于 2019-12-02 06:43:05
Is it possible with to constrain a generic parameter to be one of the select few types without figuring out what traits precisely define those type? e.g. impl<T> Data<T> where T == u32 || T == u64 Sometimes it's tedious to figure out what all traits to add to where to get the types you want, and sometimes one wouldn't want to allow a type even when it makes syntactic sense because of semantics. Peter Hall You could use a marker trait for the types you want to support: trait DataSupported {} impl DataSupported for u64 {} impl DataSupported for u32 {} impl<T> Data<T> where T: DataSupported {} As

Why is it necessary to add redundant trait bounds even though my trait uses those same traits as bounds?

蓝咒 提交于 2019-12-02 06:32:18
I have been trying to code a trait which requires a type to implement Add (and further down the line other operations for vector spaces) with itself as well as among its references. The following is a small example, illustrating the problem I ran into: use std::ops::Add; #[derive(Debug)] struct MyVec<T>(Vec<T>); impl<'a, 'b, T: Copy + Add> Add<&'a MyVec<T>> for &'b MyVec<T> { type Output = MyVec<T::Output>; fn add(self, other: &'a MyVec<T>) -> Self::Output { /* ... */ } } impl<'a, T: Copy + Add> Add<MyVec<T>> for &'a MyVec<T> { /* ... */ } impl<'a, T: Copy + Add> Add<&'a MyVec<T>> for MyVec<T>

iOS Use Vary For Traits Between iPhone Sizes

元气小坏坏 提交于 2019-12-02 05:25:42
I have a button that I want to be different widths depending on which kind of iPhone is being used. Obviously a larger width for the 7 Plus, and smaller on down the line. I click the button, click Vary for Traits, choose Height, change constraints for each iPhone, then click Done Varying and build, but it always keeps whatever the last constraint I edited was, no matter which device I run it on. Vary For Traits has nothing to do with different iPhone sizes. All iPhones (in portrait) have identical traits; there is nothing to vary. 来源: https://stackoverflow.com/questions/41710979/ios-use-vary

Do I have to implement a trait twice when implementing it for both reference and non-reference types?

匆匆过客 提交于 2019-12-02 04:46:35
I want to implement a trait for both a for reference and non-reference type. Do I have to implement the functions twice, or this is not idiomatic to do so? Here's the demo code: struct Bar {} trait Foo { fn hi(&self); } impl<'a> Foo for &'a Bar { fn hi(&self) { print!("hi") } } impl Foo for Bar { fn hi(&self) { print!("hi") } } fn main() { let bar = Bar {}; (&bar).hi(); &bar.hi(); } This is a good example for the Borrow trait. use std::borrow::Borrow; struct Bar; trait Foo { fn hi(&self); } impl<B: Borrow<Bar>> Foo for B { fn hi(&self) { print!("hi") } } fn main() { let bar = Bar; (&bar).hi();

Derive a Trait for particular variants

泪湿孤枕 提交于 2019-12-02 03:32:01
问题 Let's say I have the following Enum enum MyEnum { VariantA, VariantB, VariantC, } I can derive the PartialEq trait for the whole enum by doing so #[derive(PartialEq)] enum MyEnum { VariantA, VariantB, VariantC, } What I want to do, is derive the trait but only to particular variants and not the whole enum. Is that possible? (or does it even make sense?). 回答1: Assuming you have a setup like: #[derive(PartialEq)] struct VarB{ pub value: u32, } #[derive(PartialEq)] enum MyEnum{ VarA(VarA), VarB