traits

How do you create a generic function in Rust with a trait requiring a lifetime?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-30 10:06:08
问题 I am trying to write a trait which works with a database and represents something which can be stored. To do this, the trait inherits from others, which includes the serde::Deserialize trait. trait Storable<'de>: Serialize + Deserialize<'de> { fn global_id() -> &'static [u8]; fn instance_id(&self) -> Vec<u8>; } struct Example { a: u8, b: u8 } impl<'de> Storable<'de> for Example { fn global_id() -> &'static [u8] { b"p" } fn instance_id(&self) -> Vec<u8> { vec![self.a, self.b] } } Next, I am

Implementing Ord for a type is awkward?

岁酱吖の 提交于 2019-12-30 08:10:37
问题 I have a newtype and I want to implement Ord : use std::cmp::{Ord, Ordering}; struct MyType(isize); impl Ord for MyType { fn cmp(&self, &other: Self) -> Ordering { let MyType(ref lhs) = *self; let MyType(ref rhs) = *other; lhs.cmp(rhs) } } When I try to compare two variables of my types, I get errors: error[E0277]: the trait bound `MyType: std::cmp::PartialOrd` is not satisfied --> src/main.rs:5:6 | 5 | impl Ord for MyType { | ^^^ can't compare `MyType` with `MyType` | = help: the trait `std:

ES 6 Classes - Mixins

最后都变了- 提交于 2019-12-29 09:42:11
问题 I'm coming up with View (HTML markup) and Utility (JavaScript - behavior) architecture and creating atomic classes for composing views and utilities using ES6 Class. There will be a need that multiple utility classes can be composed/mixed into a single view class. How can ES6 Class API provide a way to mix-in class(es) into another/main class. I've looked at Object.assign but that is for objects and not at class-level. 回答1: JavaScript classes right now and hopefully also in future only can be

Define variable b of the same type as variable a

你。 提交于 2019-12-29 06:36:26
问题 Is it possible to declare a variable var_b of the same type as another variable, var_a ? For example: template <class T> void foo(T t) { auto var_a = bar(t); //make var_b of the same type as var_a } F_1 bar(T_1 t) { } F_2 bar(T_2 t) { } 回答1: Sure, use decltype : auto var_a = bar(t); decltype(var_a) b; You can add cv-qualifiers and references to decltype specifiers as if it were any other type: const decltype(var_a)* b; 回答2: decltype(var_a) var_b; And a Lorem Ipsum to reach the required

java traits or mixins pattern?

限于喜欢 提交于 2019-12-29 05:50:08
问题 Is there a way to emulate mixins or traits in java? basically, I need a way to do multiple inheritance so I can add common business logic to several classes 回答1: I would encapsulate all of the business logic into a new class BusinessLogic and have each class that needs BusinessLogic make calls to the class. If you need a single rooted heirarchy for your classes that make calls to BusinessLogic , you'll have to create an interface as well ( BusinessLogicInterface ?) In pseudo-code: interface

Trait implementation for both a trait object and for direct implementors of the trait

▼魔方 西西 提交于 2019-12-28 04:20:09
问题 I have a struct that mostly encapsulates a vector: struct Group<S> { elements: Vec<S> } I also have a simple trait which is also implemented for other structs: trait Solid { fn intersect(&self, ray: f32) -> f32; } I want to implement Solid for Group , but I want to be able to use Group both for lists of the same implementation of Solid and for lists of mixed implementations of Solid . Basically I want to use both Group<Box<Solid>> and Group<Sphere> ( Sphere implements Solid ). Currently I am

What is the cause of this overload resolution headache?

淺唱寂寞╮ 提交于 2019-12-25 05:38:35
问题 I've got a program where I've got a lot of nested if/switch statements which were repeated in several places. I tried to extract that out and put the switches in a template method class, and then allow clients to overload which switch branches they wanted to specifically handle using overloading: class TraitsA {}; class TraitsB : public TraitsA {}; class Foo { bool traitsB; public: // Whether or not a Foo has traitsB is determined at runtime. It is a // function of the input to the program

Referencing the same trait in another trait and class

ⅰ亾dé卋堺 提交于 2019-12-25 03:59:26
问题 PHP seems to be trying to compile the same trait twice. use Behat\MinkExtension\Context\MinkDictionary; class FeatureContext { use MinkDictionary, OrderDictionary; } use Behat\MinkExtension\Context\MinkDictionary; trait OrderDictionary { //if you comment out this line, everything works, but methodFromMinkTrait is //unresolved use MinkDictionary; public function myMethod($element, $text) { //some method that uses methods from MinkDictionary return $this->methodFromMinkTrait(); } } The

What type annotations does Rust want for this UFCS call?

家住魔仙堡 提交于 2019-12-24 15:10:03
问题 Sorry, I'm probably missing something super obvious. I wonder why I can't call my trait method like this. Shouldn't this be the standard UFCS. trait FooPrinter { fn print () { println!("hello"); } } fn main () { FooPrinter::print(); } Playpen: http://is.gd/ZPu9iP I get the following error error: type annotations required: cannot resolve `_ : FooPrinter` 回答1: You cannot call a trait method without specifying on which implementation you wish to call it. It doesn't matter that the method has a

clone_ptr problem, I need to create a copy object using a function of the library instead of new

风格不统一 提交于 2019-12-24 11:26:52
问题 I am a bit new to templates in C++ so forgive me if this question is confusing or stupid, I just have a problem where I want to implement a clone smart pointer so I don't have to create copy constructors for each and every class that uses my underlying XML library that only seems to use object pointers and not smart pointers. The problem is that my traits need to create the new objects using functions from the underlying library and I do not know how I would go about doing that in a template