traits

Curiously recurring generic trait pattern: overflow evaluating the requirement

社会主义新天地 提交于 2019-12-04 06:34:17
问题 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

PHP traits - change value of static property in inherited class

泄露秘密 提交于 2019-12-04 05:00:14
So, this is my trait: trait Cacheable { protected static $isCacheEnabled = false; protected static $cacheExpirationTime = null; public static function isCacheEnabled() { return static::$isCacheEnabled && Cache::isEnabled(); } public static function getCacheExpirationTime() { return static::$cacheExpirationTime; } } This is the base class: abstract class BaseClass extends SomeOtherBaseClass { use Cacheable; ... } These are my 2 final classes: class Class1 extends BaseClass { ... } class Class2 extends BaseClass { protected static $isCacheEnabled = true; protected static $cacheExpirationTime =

Can #[inline] be used in both trait method declarations and implementations?

隐身守侯 提交于 2019-12-04 04:45:30
问题 I have a trait with some small methods, which are generally implemented as one-line wrappers around other methods that the implementing structs have. If I want to make sure that the trait method is inlined, should I place #[inline(always)] inside the trait definition, or inside the impl for each struct? I'd prefer to simply put it in the trait definition, but as far as I can tell that doesn't work. 回答1: What does inline mean? When a compiler inlines a call, it copies the body of the function

Cannot call rusqlite's query because it expects the type &[&rusqlite::types::ToSql]

孤者浪人 提交于 2019-12-04 04:42:53
问题 I want to use a prepared statement with rusqlite. Rusqlite implements the trait ToSql for String , &str and a bunch of other types: extern crate rusqlite; use rusqlite::Connection; fn main() { let mut connection = Connection::open("C:\\test_db.db").unwrap(); let mut cached_statement = connection .prepare_cached("SELECT ?, ?, ? FROM test") .unwrap(); let vec_values = vec![ &"test1".to_string(), &"test2".to_string(), &"test3".to_string(), ]; let rows = cached_statement.query(vec_values.as_slice

How do I implement generic commutative std::ops involving a builtin type for trait objects?

泄露秘密 提交于 2019-12-04 04:21:16
问题 I have: use std::ops::{Add, Div, Mul, Neg, Sub}; pub trait Hilbert: Add + Sub + Mul + Div + Neg + Mul<f64, Output = Self> + Div<f64, Output = Self> + Sized + Copy { fn dot(&self, other: &Self) -> f64; fn magnitude(&self) -> f64; } fn g<T: Hilbert>(x: T) -> f64 { let a = (x * 2.0).dot(&x); let b = (2.0 * x).dot(&x); a + b } error[E0277]: cannot multiply `T` to `{float}` --> src/main.rs:12:18 | 12 | let b = (2.0 * x).dot(&x); | ^ no implementation for `{float} * T` | = help: the trait `std::ops

Why method renaming does not work in PHP traits?

冷暖自知 提交于 2019-12-04 03:50:19
I use PHP 7.1.0. Let's say we have a trait, we use it inside a class and rename the imported method: trait T { public function A() { echo "."; } } class C { use T { A as B; } } $c = new C(); $c->B(); $c->A(); // Why does it work? Why does PHP still allow me to use old method name ( A in this case)? It's really a pain because in more complex examples you cannot rely on method renaming - and thus you can unexpectedly receive "incompatible declarations" error: class BaseSrc { } trait BaseTrait { public function init(BaseSrc $baseSrc) { echo "Init Base"; } } class Base { use BaseTrait { BaseTrait:

What are stackable modifications?

拈花ヽ惹草 提交于 2019-12-04 03:47:09
I've been reading a book about Scala and there's mention of stackable modifications using traits . What are stackable modifications and for what purposes are they meant to be used? The fundamental quality which distinguishes stackable modifications (as the terminology is used in scala anyway) is that "super" is influenced dynamically based on how the trait is mixed in, whereas in general super is a statically determined target. If you write abstract class Bar { def bar(x: Int): Int } class Foo extends Bar { def bar(x: Int) = x } then for Foo "super" will always be Bar. If you write trait Foo1

Trait `x` is not implemented for the type `x`

梦想与她 提交于 2019-12-04 03:11:27
问题 When compiling the following code: trait RenderTarget {} struct RenderWindow; impl RenderTarget for RenderWindow {} trait Drawable { fn draw<RT: RenderTarget>(&self, target: &mut RT); } fn main() { let mut win = RenderWindow; let mut vec: Vec<Box<Drawable>> = Vec::new(); for e in &vec { e.draw(&mut win); } } I get the error: error: the trait `Drawable` is not implemented for the type `Drawable` [E0277] src/main.rs:15 e.draw(&mut win); ^~~~~~~~~~~~~~ What is the error message trying to tell?

Ways to achieve effective Java traits?

空扰寡人 提交于 2019-12-04 01:43:03
Please let me know if this is inappropriate as formulated (in particular whether Programmers.SE or something would be better for the question.) Alright. So I've got a number of 'traits' that I'm currently expressing as interfaces. Let's call them "updatable" and "destructible". Expressing them as interfaces has the downside that I can't share behavior between all "destructible" components; on the other hand, expressing these as abstract classes mean I can't mix and match without explicitly defining the mixed trait as another abstract class ("UpdateableAndDestructible") and furthermore this

Providing an implementation when both trait and type are not in this crate [duplicate]

二次信任 提交于 2019-12-04 01:18:57
问题 This question already has answers here : How do I implement a trait I don't own for a type I don't own? (2 answers) Closed 4 years ago . I want to provide an implementation of a trait ToHex (not defined by me, from serialize ) for a primitive type u8 : impl ToHex for u8 { fn to_hex(&self) -> String { self.to_str_radix(16) } } The problem is I get this compiler error: error: cannot provide an extension implementation where both trait and type are not defined in this crate I understand the