traits

How can I implement Ord when the comparison depends on data not part of the compared items?

泪湿孤枕 提交于 2021-01-26 20:38:07
问题 I have a small struct containing only an i32 : struct MyStruct { value: i32, } I want to implement Ord in order to store MyStruct in a BTreeMap or any other data structure that requires you to have Ord on its elements. In my case, comparing two instances of MyStruct does not depend on the value s in them, but asking another data structure (a dictionary), and that data structure is unique for each instance of the BTreeMap I will create. So ideally it would look like this: impl Ord for MyStruct

Size of a box containing a struct with a trait parameter

半世苍凉 提交于 2021-01-04 06:53:34
问题 I need a struct that contains a trait object and more of itself. Disappointedly the following code does not compile: trait Foo {} struct Bar<T: Foo> { bars: Vec<Box<Bar<dyn Foo>>>, foo: T, } I managed to coerce this into compiling by adding the ?Sized bound to T , but I do not understand why this should be the case. I assume this is because all trait objects have the same size, but the size of Bar depends on the size of the concrete type T . If so, how is Bar with an unsized T represented in

Mixin to wrap every method of a Scala trait

时光总嘲笑我的痴心妄想 提交于 2020-12-29 06:58:07
问题 Suppose I have a trait Foo with several methods. I want to create a new trait which extends Foo but "wraps" each method call, for example with some print statement (in reality this will be something more complicated / I have a couple of distinct use cases in mind). trait Foo { def bar(x: Int) = 2 * x def baz(y: Int) = 3 * y } I can do this manually, by overriding each method. But this seems unnecessarily verbose (and all too easy to call the wrong super method): object FooWrapped extends

Is there an intrinsic reason explaining why Rust does not have higher-kinded-types?

岁酱吖の 提交于 2020-12-29 05:54:43
问题 Rust does not have higher-kinded-types. For example, functor (and thus monad) cannot be written in Rust. I would like to know if there is a deep reason explaining this and why. For instance, reason that I can understand can be that there is no zero-cost abstraction making HKT possible. Or type inference is significantly more difficult. And of course, I also looking for an explaination showing me why this is a real limitation. If the anwer was already given somewhere else, could you give me

Is there an intrinsic reason explaining why Rust does not have higher-kinded-types?

拜拜、爱过 提交于 2020-12-29 05:53:47
问题 Rust does not have higher-kinded-types. For example, functor (and thus monad) cannot be written in Rust. I would like to know if there is a deep reason explaining this and why. For instance, reason that I can understand can be that there is no zero-cost abstraction making HKT possible. Or type inference is significantly more difficult. And of course, I also looking for an explaination showing me why this is a real limitation. If the anwer was already given somewhere else, could you give me

Is there an intrinsic reason explaining why Rust does not have higher-kinded-types?

帅比萌擦擦* 提交于 2020-12-29 05:53:28
问题 Rust does not have higher-kinded-types. For example, functor (and thus monad) cannot be written in Rust. I would like to know if there is a deep reason explaining this and why. For instance, reason that I can understand can be that there is no zero-cost abstraction making HKT possible. Or type inference is significantly more difficult. And of course, I also looking for an explaination showing me why this is a real limitation. If the anwer was already given somewhere else, could you give me

How to implement the Debug trait for struct that has a trait object member? [duplicate]

眉间皱痕 提交于 2020-12-26 09:16:45
问题 This question already has answers here : Can I get a trait object of a multi-trait instance without using a generic type? (1 answer) Is there any way to create a type alias for multiple traits? (2 answers) Closed last year . My goal is printing the contents of struct that has trait object member but I can't find how to tell Rust compiler that the member also implements other traits like Display or Debug . For example, in the following program, I want to print the structure of S2 (and S1 for

How to implement the Debug trait for struct that has a trait object member? [duplicate]

心不动则不痛 提交于 2020-12-26 09:15:15
问题 This question already has answers here : Can I get a trait object of a multi-trait instance without using a generic type? (1 answer) Is there any way to create a type alias for multiple traits? (2 answers) Closed last year . My goal is printing the contents of struct that has trait object member but I can't find how to tell Rust compiler that the member also implements other traits like Display or Debug . For example, in the following program, I want to print the structure of S2 (and S1 for

How to implement the Debug trait for struct that has a trait object member? [duplicate]

假装没事ソ 提交于 2020-12-26 09:14:56
问题 This question already has answers here : Can I get a trait object of a multi-trait instance without using a generic type? (1 answer) Is there any way to create a type alias for multiple traits? (2 answers) Closed last year . My goal is printing the contents of struct that has trait object member but I can't find how to tell Rust compiler that the member also implements other traits like Display or Debug . For example, in the following program, I want to print the structure of S2 (and S1 for

Generic implementation depending on traits

假如想象 提交于 2020-12-23 11:50:48
问题 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