traits

Passing a closure that modifies its environment to a function in Rust

≯℡__Kan透↙ 提交于 2020-01-04 07:36:41
问题 I have a closure that captures and modifies its environment. I want to pass this closure to a function that accepts closures: fn main() { let mut integer = 5; let mut closure_variable = || -> i32 { integer += 1; integer }; execute_closure(&mut closure_variable); } fn execute_closure(closure_argument: &mut Fn() -> i32) { let result = closure_argument(); println!("Result of closure: {}", result); } Because the closure modifies its environment, this fails: error[E0525]: expected a closure that

Can't access fields of structs in implementations of dynamic traits [duplicate]

筅森魡賤 提交于 2020-01-04 05:37:36
问题 This question already has answers here : Expected type parameter, found u8, but the type parameter is u8 (2 answers) Closed 3 years ago . While trying to implement traits with generic arguments and access the fields of those generic arguments, I've encountered an error message saying that the arguments in question do not contain such fields. Here's some example code that exhibits the issue: pub struct Settings { pub time: String, } pub trait Foo { fn get<T>(t: T); } struct Bar; impl Foo for

How can I get a list of types that implement a particular trait in Rust?

你说的曾经没有我的故事 提交于 2020-01-04 04:09:08
问题 I want to know a struct that implements std::io::Write ; is it described in some document? 回答1: When you lookup the API for std you can search for your trait there (e.g. std::io::Write). When you scroll down to the section "Implementors" you will see all structs/enums that implement that trait in std . To get a better overview, you can use the + or - keys to collapse all sections and have a nice overview, e.g. 来源: https://stackoverflow.com/questions/55216559/how-can-i-get-a-list-of-types-that

Converting an enum where all variants implement the same trait to a box in Rust?

被刻印的时光 ゝ 提交于 2020-01-04 04:05:08
问题 I have a trait Foo , with some implementations, together with an enum Foos with one variant per implemention. I want to be able to convert my enum into Box<dyn Foo> . This is my current solution: trait Foo {} struct FooA {} impl Foo for FooA {} struct FooB {} impl Foo for FooB {} struct FooC {} impl Foo for FooC {} enum Foos { A(FooA), B(FooB), C(FooC), } impl Foos { fn into_box(self) -> Box<dyn Foo> { match self { Foos::A(foo) => Box::new(foo), Foos::B(foo) => Box::new(foo), Foos::C(foo) =>

PHP: Is it possible to get the name of the class using the trait from within a trait static method?

徘徊边缘 提交于 2020-01-04 03:06:41
问题 Can the name of a class using a trait be determined from within a static method belonging to that trait? For example: trait SomeAbility { public static function theClass(){ return <name of class using the trait>; } } class SomeThing { use SomeAbility; ... } Get name of class: $class_name = SomeThing::theClass(); My hunch is, probably not. I haven't been able to find anything that suggests otherwise. 回答1: Use late static binding with static: trait SomeAbility { public static function theClass(

PHP: Is it possible to get the name of the class using the trait from within a trait static method?

强颜欢笑 提交于 2020-01-04 03:06:28
问题 Can the name of a class using a trait be determined from within a static method belonging to that trait? For example: trait SomeAbility { public static function theClass(){ return <name of class using the trait>; } } class SomeThing { use SomeAbility; ... } Get name of class: $class_name = SomeThing::theClass(); My hunch is, probably not. I haven't been able to find anything that suggests otherwise. 回答1: Use late static binding with static: trait SomeAbility { public static function theClass(

Type aliases and incomplete types

瘦欲@ 提交于 2020-01-03 17:05:40
问题 I'm probably over reaching here to solve what should be a simple problem. I started this question here: Getting type of base class at compile time Basically I'm trying to make the class manage it's own pointer types. I'm wrapping a C library where some structures have reference counting embedded in them, and others do not. Those that don't, I'd like to use shared_ptr. Those that do, I'd like to use intrusive_ptr. I'd like to avoid relying on programmer intellect to ensure the use of the

Scala method = trait { … } meaning

痴心易碎 提交于 2020-01-03 16:57:11
问题 I'm trying to learn Scala and the Play Framework at the same time. Scala looks to me like it has a lot of really cool ideas, but one of my frustrations is trying to understand all of the different syntaxes for methods/functions/lambdas/anonymous functions/etc. So I have my main application controller like so: object Application extends Controller { def index = Action { Ok(views.html.index("Your new application is ready.")) } } This tells me I have a singleton Application that has one method,

Stackoverflow when use bufferedInputStream as a trait in scala

蓝咒 提交于 2020-01-03 16:49:04
问题 I am trying to solve a problem given by the Book "Scala for the Impatient", which asked to implement java's BufferedInputStream as a trait. Here is my implementation, trait Buffering { this:InputStream => private[this] val bis = { new JavaBufferedInputStream(this) } override def read = bis.read override def read(byte:Array[Byte], off:Int, len:Int) = bis.read(byte, off, len) override def available = bis.available override def close() { bis.close } override def skip(n:Long) = bis.skip(n) } def

type to int mapping

做~自己de王妃 提交于 2020-01-03 13:41:07
问题 I have two c++ programs that need to have a map type -> int that is known at compile time and equal between the two programs. Furthermore, I'd like to automatically make sure at compile time that the map is one-to-one. How would you solve that? (c++0x-extensions are allowed). The first part is easy: Share a template < typename T > struct map; template <> struct map <...> { enum { val = ...; }; }; between the programs. (The second part means that I don't want to accidently define the same val