traits

Rust Vector of Traits: cast each trait

匆匆过客 提交于 2019-12-11 07:23:38
问题 I have a problem casting a vector of traits into a vector of different traits. Using the approach of Type-casting arrays/vectors in Rust , I basically tried the following: trait ParentTrait {} trait ChildTrait: ParentTrait {} fn main() { let mut children: Vec<Box<ChildTrait>> = vec![]; let parents = children.iter().map(|&e| e as Box<ParentTrait>); } Now this does not compile, it results in error: the trait `core::kinds::Sized` is not implemented for the type `ChildTrait` [...] error: the

Can a trait guarantee certain type properties such as a vector is non-empty?

。_饼干妹妹 提交于 2019-12-11 07:22:06
问题 Imagine I have functions like this: fn min_max_difference(row: &Vec<u32>) -> u32 { let mut min_elem: u32 = row[0]; let mut max_elem: u32 = min_elem; for &element in row.iter().skip(1) { if element < min_elem { min_elem = element; } else if element > max_elem { max_elem = element; } } result = max_elem - min_elem; } fn execute_row_operation(row: &Vec<u32>, operation: Fn(&Vec<u32>) -> u32) -> Option<(u32, u32)> { let mut result = None; if row.len() > 0 { result = operation(row); } result } Note

PHP Trait Override Protected Trait Method

青春壹個敷衍的年華 提交于 2019-12-11 06:35:32
问题 I am having problems with a composer package I am dealing with. It implements a trait Billable. trait Billable { /** * Update the payment method token for all of the user's subscriptions. * * @param string $token * @return void */ protected function updateSubscriptionsToPaymentMethod($token) { foreach ($this->subscriptions as $subscription) { if ($subscription->active()) { BraintreeSubscription::update($subscription->braintree_id, [ 'paymentMethodToken' => $token, ]); } } } } I am trying to

Define 'copy' method in trait for case class

筅森魡賤 提交于 2019-12-11 03:53:24
问题 Given simplified code example: sealed trait A { val c1: String val c2: Int def copy[Z <: A](src: File) : Z } case class B(c1: String, c2: Int, src: File) extends A case class C(c1: String, c2: Int, c3: MyClass, src: File) extends A how do I define copy method in trait A so it will match generated one for case class and 'target' file? Given definition does typecheck and complains about missing method copy in classes B and C. 回答1: scala compiler will not generate copy methods for case class

Usage of noexcept in derived classes

一曲冷凌霜 提交于 2019-12-11 03:48:52
问题 I encounter an issue while using the noexcept specifier on derived classes, more precisely when the parent is an abstract class (has protected constructors). Hereafter is an example of the way I declare my classes. With a public constructor in the base class: Everything is ok. Same code with protected and the derived class is no more "nothrow movable". Do I miss something? Is std::is_nothrow_move_constructible the correct traits to use in derived class declarations or should I use something

How to get Deref coercion when using impl Trait (take 2)

心已入冬 提交于 2019-12-11 03:36:14
问题 Here is a trait (simplified for the question) which I'd like to implement for every type that behaves like a slice: trait SliceLike { type Item; /// Computes and returns (owned) the first item in a collection. fn first_item(&self) -> Self::Item; } Note that the Item type is an associated type; I want each type that is SliceLike to have a unique element type. Here is an attempt at a blanket implementation: use std::ops::Deref; impl<T: Clone, U: Deref<Target = [T]>> SliceLike for U { type Item

Object extends Trait, Class extends Trait, both have to implement method

﹥>﹥吖頭↗ 提交于 2019-12-11 03:33:18
问题 I have the following setup: trait A { def doSomething(): Unit; } object B extends A { override def doSomething(): Unit = { // Implementation } } class B(creator: String) extends A { override def doSomething(): Unit = { B.doSomething() // Now this is just completely unnecessary, but the compiler of course insists upon implementing the method } } Now you may wonder why I even do this, why I let the class extend the trait as well. The problem is, that somewhere in the Program there is a

Rust and serde deserializing using generics

假如想象 提交于 2019-12-11 03:19:41
问题 I am trying to use generics to deserialize structs from file for use with a Swagger generated API. So I have hacked together this which almost works, but I am unable to unpack the external Struct object from the "Owned" pointer, as you can see in the tests. This might be the wrong strategy, but the problem is that I have various yaml files, which I want to read in and deserialise hinting the correct Struct to deserialise as. I don't want to implement a "readfile" function for each Struct, as

Rust trait object's &self cannot be used in a trait default function

风格不统一 提交于 2019-12-11 02:48:59
问题 Trying to override a trait cast problem, described here. Stuck at implementing the trait function which returns the enum instance with own implementation inside: //the "trait matcher" enum enum Side<'a> { Good(&'a GoodDude), Bad(&'a BadDude), } //very general trait trait Dude { fn who_am_i(&self) -> Side; fn do_useful_stuff(&self); } //specific trait #1 trait GoodDude: Dude { fn who_am_i_inner(&self) -> Side { Side::Good(&self) } fn save_the_world(&self); } //specific trait #2 trait BadDude:

How to populate the modified_by with the user_id of who made the update to the record using laravel 5.1?

拥有回忆 提交于 2019-12-11 02:45:56
问题 When using Laravel 5.1, I am trying to create an Observer that will automatically update the following 3 columns created_by: populate when the record in created "never update this again" modified_by: populate a new value every time a record is modified purged_by: populate a value when a record is soft deleted. I understand that Eloquent will automatically update the date time stamp (created_by, updated_at, removed_at) but I need to update the user_id that made the change. I was advised to use