traits

How to have multiple traits of same base trait implement the same method

自闭症网瘾萝莉.ら 提交于 2021-01-29 11:21:19
问题 I have a scenario with multiple traits inheriting from a fixed base trait. The base trait has an abstract method that each needs to implement. The class using these traits also needs to implement this method: trait A { def f: Any } trait B1 extends A { val i: Int override def f: Any = println("B1: " + i) } trait B2 extends A { val j: Int override def f: Any = println("B2: " + j) } class C extends A with B1 with B2 { val i = 1 val j = 2 override def f: Any = { super.f println("C::f") } } Then

How can I implement a trait in scope for an enum of existing types for which the trait is implemented?

人走茶凉 提交于 2021-01-29 11:10:03
问题 How can I implement a trait in scope for an enum of existing types for which the trait is implemented? I have this: extern crate pnet; use pnet::packet::ipv4::Ipv4Packet; use pnet::packet::ipv6::Ipv6Packet; enum EthernetType { IPv4, ARP, VLAN, IPv6, Unknown(u16), } enum IPPacket<'a> { IPv4(Ipv4Packet<'a>), IPv6(Ipv6Packet<'a>), } fn ip_decode(pkt: &[u8]) -> IPPacket { let version = (pkt[0] & 0xf0) >> 4; if version == 4 { IPPacket::IPv4(Ipv4Packet::new(&pkt).unwrap()) } else { IPPacket::IPv6

Why does this TypeScript mixin employing generics fail to compile?

南笙酒味 提交于 2021-01-29 04:04:20
问题 I'm using mixins/traits with TypeScript using a subclass factory pattern as described at https://mariusschulz.com/blog/mixin-classes-in-typescript. The trait in question is called Identifiable , which imparts an id property to a class that should express the Identifiable trait. When I attempt to use the trait with another, non-generic trait ( Nameable ) in a certain order, compilation fails. class Empty {} type ctor<T = Empty> = new(...args: any[]) => T; function Nameable<T extends ctor =

Is there a subtrait of `Index` that specifies the `len` method?

元气小坏坏 提交于 2021-01-29 03:59:59
问题 The std::ops::Index trait is implemented by types that support array subscript notation. It appears that most types that implement Index also have a len method, but it is not part of the trait so you can't assume it exists. Therefore, I find myself writing code specialized for slices (which do have a len method), but I would prefer to be more general. Is there a subtrait of Index that specifies the len method or in some other way reveals what range of indices is allowed? 回答1: Is there a

Array cannot be indexed by RangeFull?

两盒软妹~` 提交于 2021-01-29 03:52:56
问题 Consider the following example: use std::ops::Index; use std::ops::RangeFull; fn f<T: Index<RangeFull>>(x: T) {} fn main() { let x: [i32; 4] = [0, 1, 2, 3]; f(x); } Upon calling f(x) , I get an error: error[E0277]: the type `[i32; 4]` cannot be indexed by `std::ops::RangeFull` --> src/main.rs:8:5 | 8 | f(x); | ^ `[i32; 4]` cannot be indexed by `std::ops::RangeFull` | = help: the trait `std::ops::Index<std::ops::RangeFull>` is not implemented for `[i32; 4]` note: required by `f` --> src/main

Array cannot be indexed by RangeFull?

家住魔仙堡 提交于 2021-01-29 03:45:56
问题 Consider the following example: use std::ops::Index; use std::ops::RangeFull; fn f<T: Index<RangeFull>>(x: T) {} fn main() { let x: [i32; 4] = [0, 1, 2, 3]; f(x); } Upon calling f(x) , I get an error: error[E0277]: the type `[i32; 4]` cannot be indexed by `std::ops::RangeFull` --> src/main.rs:8:5 | 8 | f(x); | ^ `[i32; 4]` cannot be indexed by `std::ops::RangeFull` | = help: the trait `std::ops::Index<std::ops::RangeFull>` is not implemented for `[i32; 4]` note: required by `f` --> src/main

Problem with generic traits and lifetimes

巧了我就是萌 提交于 2021-01-28 21:55:36
问题 I had a problem with generic traits in a larger context and try to size it down to this smaller problem. I want to have following function: fn count<I, S, T>(pattern: T, item:I) -> usize where I: Atom, S: Iterator<Item = I> T: Atoms<I, S>, { pattern.atoms().filter(|i| i == &item).count() } The function should be passed two arguments: pattern:Atoms<...> which has a factory method atoms returning an iterator of atoms item:Atom which is the item that should be counted in the iterator of atoms

Call explicit constructor/destructor with traits in templatized function

跟風遠走 提交于 2021-01-28 19:59:51
问题 I'm trying to call explicit constructor/destructor with traits in templatized function. template <int i> struct Traits { }; template <> struct Traits<1> { typedef Foo type_t; }; template <> struct Traits<2> { typedef Bar type_t; }; template <class Traits> void DoSomething(void* p_in) { typename Traits::type_t* p = reinterpret_cast<typename Traits::type_t*>(p_in); // this works. new (p) typename Traits::type_t; // neither of following two does work. p->~typename Traits::type_t(); p->typename

Generic associated type may not live long enough

心已入冬 提交于 2021-01-28 13:50:44
问题 Take the following example (Playground): #![feature(generic_associated_types)] #![allow(incomplete_features)] trait Produce { type CustomError<'a>; fn produce<'a>(&'a self) -> Result<(), Self::CustomError<'a>>; } struct GenericProduce<T> { val: T, } struct GenericError<'a, T> { producer: &'a T, } impl<T> Produce for GenericProduce<T> { type CustomError<'a> = GenericError<'a, T>; fn produce<'a>(&'a self) -> Result<(), Self::CustomError<'a>> { Err(GenericError{producer: &self.val}) } } The

Generic associated type may not live long enough

喜欢而已 提交于 2021-01-28 13:39:07
问题 Take the following example (Playground): #![feature(generic_associated_types)] #![allow(incomplete_features)] trait Produce { type CustomError<'a>; fn produce<'a>(&'a self) -> Result<(), Self::CustomError<'a>>; } struct GenericProduce<T> { val: T, } struct GenericError<'a, T> { producer: &'a T, } impl<T> Produce for GenericProduce<T> { type CustomError<'a> = GenericError<'a, T>; fn produce<'a>(&'a self) -> Result<(), Self::CustomError<'a>> { Err(GenericError{producer: &self.val}) } } The