traits

Use final on traits in PHP

只谈情不闲聊 提交于 2021-01-28 03:30:34
问题 What i want is the ability to make "final traits" with the behaviour as described below. I realise this is not possible with traits(or is it? that would make me so happy), but I'm just trying to convey what I want to do. So, i want to have a trait that is trait Content { public final function getPostContent(){ /*...*/ } public final function setPostContent($content){ /*...*/ } } What I want is Marking the functions in the traits as final making sure that if a class uses this trait, the trait

Use final on traits in PHP

爱⌒轻易说出口 提交于 2021-01-28 02:14:01
问题 What i want is the ability to make "final traits" with the behaviour as described below. I realise this is not possible with traits(or is it? that would make me so happy), but I'm just trying to convey what I want to do. So, i want to have a trait that is trait Content { public final function getPostContent(){ /*...*/ } public final function setPostContent($content){ /*...*/ } } What I want is Marking the functions in the traits as final making sure that if a class uses this trait, the trait

Can I force a trait to be covariant?

跟風遠走 提交于 2021-01-27 16:57:11
问题 Thanks to @francis-gagné 's excellent answer to another question, I have a clearer view of how variance works. For example, a type containing a reference is covariant over its lifetime parameter, as demonstrated below. struct Foo<'a> (PhantomData<&'a str>); /// Foo is covariant over its lifetime parameter pub fn test_foo<'a:'b, 'b:'c, 'c>() { let fa: Foo<'a> = Foo(PhantomData); let fb: Foo<'b> = Foo(PhantomData); let fc: Foo<'c> = Foo(PhantomData); let v: Vec<Foo<'b>> = vec![fa, fb]; // fc is

Can I force a trait to be covariant?

女生的网名这么多〃 提交于 2021-01-27 16:51:27
问题 Thanks to @francis-gagné 's excellent answer to another question, I have a clearer view of how variance works. For example, a type containing a reference is covariant over its lifetime parameter, as demonstrated below. struct Foo<'a> (PhantomData<&'a str>); /// Foo is covariant over its lifetime parameter pub fn test_foo<'a:'b, 'b:'c, 'c>() { let fa: Foo<'a> = Foo(PhantomData); let fb: Foo<'b> = Foo(PhantomData); let fc: Foo<'c> = Foo(PhantomData); let v: Vec<Foo<'b>> = vec![fa, fb]; // fc is

Specifying associated type in trait that inherits from another trait

不问归期 提交于 2021-01-27 15:01:37
问题 I started working on my first more ambitious Rust project, and struggle with something I did not come across in any of the resources and tutorials I used for learning. The title of the question captures the abstract problem, but for the examples I'll use the concrete examples I am fighting with. For my project, I need to interface with different third-party services, and I decided to use the actix framework as an abstraction for the different actors in my domain. The framework defines the

How can I use internal mutability with generic type in Rust?

家住魔仙堡 提交于 2021-01-27 11:43:11
问题 I would like to design a struct in Rust that can be constructed with an object that implements the Digest trait, and abstract the behavior of the hash behind a method. Here's a simple example that doesn't compile: use digest::Digest; struct Crypto<D: Digest> { digest: D, } impl<D> Crypto<D> where D: Digest, { pub fn hash(&self, data: &[u8]) -> Vec<u8> { self.digest.chain(&data).finalize_reset().to_vec() } } This fails to compile because self is immutably borrowed in the method signature, so

Why can't I push into a Vec of dyn Trait unless I use a temporary variable?

余生长醉 提交于 2021-01-27 06:29:48
问题 This is my code: use std::rc::{Rc, Weak}; use std::cell::RefCell; trait Trait {} fn push<E: Trait>(e: E) { let mut v: Vec<Rc<RefCell<Box<dyn Trait>>>> = Vec::new(); // let x = Rc::new(RefCell::new(Box::new(e))); // v.push(x); // error v.push(Rc::new(RefCell::new(Box::new(e)))); // works fine } The v.push(x) raises this error: error[E0308]: mismatched types --> src/main.rs:12:12 | 7 | fn push<E: Trait>(e: E) { | - this type parameter ... 12 | v.push(x); | ^ expected trait object `dyn Trait`,

Why can't I push into a Vec of dyn Trait unless I use a temporary variable?

最后都变了- 提交于 2021-01-27 06:29:16
问题 This is my code: use std::rc::{Rc, Weak}; use std::cell::RefCell; trait Trait {} fn push<E: Trait>(e: E) { let mut v: Vec<Rc<RefCell<Box<dyn Trait>>>> = Vec::new(); // let x = Rc::new(RefCell::new(Box::new(e))); // v.push(x); // error v.push(Rc::new(RefCell::new(Box::new(e)))); // works fine } The v.push(x) raises this error: error[E0308]: mismatched types --> src/main.rs:12:12 | 7 | fn push<E: Trait>(e: E) { | - this type parameter ... 12 | v.push(x); | ^ expected trait object `dyn Trait`,

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

走远了吗. 提交于 2021-01-26 20:38:31
问题 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

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

拈花ヽ惹草 提交于 2021-01-26 20:38:19
问题 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