traits

Working with trait objects requiring sized

爱⌒轻易说出口 提交于 2019-12-19 09:09:56
问题 I'd like to have a LinkedList of trait object wrapper structs. The inner would be a stream type for either an Ssl or Non-Ssl stream. My hope was to pass the struct wrapper around, and as long as the inner conformed to the same trait, everything would be OK regardless of inner stream type being used. Simple example: use std::sync::{Arc, Mutex}; use std::collections::LinkedList; use std::os::unix::io::{RawFd, AsRawFd}; pub trait HRecv {} pub trait HSend {} pub trait HStream: HRecv + HSend +

Working with trait objects requiring sized

怎甘沉沦 提交于 2019-12-19 09:09:04
问题 I'd like to have a LinkedList of trait object wrapper structs. The inner would be a stream type for either an Ssl or Non-Ssl stream. My hope was to pass the struct wrapper around, and as long as the inner conformed to the same trait, everything would be OK regardless of inner stream type being used. Simple example: use std::sync::{Arc, Mutex}; use std::collections::LinkedList; use std::os::unix::io::{RawFd, AsRawFd}; pub trait HRecv {} pub trait HSend {} pub trait HStream: HRecv + HSend +

Behaviour of super in chained Scala traits

半城伤御伤魂 提交于 2019-12-19 09:04:24
问题 Why does x.func below return "B extends B extends B" ? How to arrange this code so that it returns "B extends A extends Base" ? trait Base { def name = "Base" def func = name } trait A extends Base { override def name = "A" override def func = name + " extends " + super.func } trait B extends Base { override def name = "B" override def func = name + " extends " + super.func } val x = new Base with A with B println(x.func) Update: One arrangement could be as follows. It now has identical

Why is my 'shutdown callback ' invalid when using register_shutdown_function()?

蓝咒 提交于 2019-12-19 08:10:13
问题 Warning: register_shutdown_function(): Invalid shutdown callback trait ErrorTrait { public function shutDownFunction() { $error = error_get_last(); // fatal error, E_ERROR === 1 if ($error['type'] === E_ERROR) { //do your stuff $messageStore="Using $this when not in object context"; if (strstr ( $error['message'],$messageStore)) { echo "found it"; } } } public function shutdown_function() { register_shutdown_function('shutDownFunction'); } } I use this trait in my main class and call the

How to use traits in Laravel 5.4.18?

删除回忆录丶 提交于 2019-12-19 06:46:10
问题 I need an example of where to exactly create the file, to write to it, and how to use the functions declared in the trait. I use Laravel Framework 5.4.18 -I have not altered any folder in the framework, everything is where it corresponds- From already thank you very much. 回答1: I have Create a Traits directory in my Http directory with a Trait called BrandsTrait.php and use it like: use App\Http\Traits\BrandsTrait; class YourController extends Controller { use BrandsTrait; public function

Is there a way to implement a trait on top of another trait? [duplicate]

别说谁变了你拦得住时间么 提交于 2019-12-19 06:25:07
问题 This question already has answers here : I implemented a trait for another trait but cannot call methods from both traits (2 answers) Closed 2 years ago . I am trying to create a base trait that will implement other operator traits ( Add , Subtract , Multiply , Divide , etc...) for me. This fails to compile, it looks like an issued with Sized , but even when Measurement is set to require Sized it does not work. Is this even possible? use std::ops::Add; #[derive(Copy, Clone, Debug)] struct

What is the Rust equivalent to C++'s virtual functions?

我与影子孤独终老i 提交于 2019-12-19 04:15:16
问题 I'm trying to implement something in Rust that works like a C++ virtual function in a class, I would have a base struct with data, then I would keep some functions undefined, like the following example: class A { int stuff; public: virtual void foo(int a, int b) = 0; void function_that_calls_foo() { /*...*/ foo(1, 2); /*...*/ } } class B: public A { void foo(int a, int b) { /* ... */ } } I was trying to implement it using function pointers, but without much success. I could use a trait with A

Best practice to implement Scala trait which supports method chaining

流过昼夜 提交于 2019-12-18 21:57:10
问题 I want to make a trait which add some property to a class and make it possible to chain methods. Tested in Scala 2.8.1. trait SomeProperty { var prop : String = "default" def setProp(s: String) = { prop = s this } } sealed abstract class Value case class IntegerValue(v: Int) extends Value case class FloatValue(v: Float) extends Value with SomeProperty { def foo() = { println("I'm foo.") } } case object UnknownValue extends Value with SomeProperty { def bar() = { println("I'm bar.") } } scala>

How can I create an is_prime function that is generic over various integer types?

感情迁移 提交于 2019-12-18 12:07:33
问题 I just took the dive into Rust and want to make some basic math functions that are generic. I have the following is_prime function: fn is_prime(n: i64) -> bool { if n == 2 || n == 3 { return true; } else if n % 2 == 0 || n % 3 == 0 { return false; } let mut i = 5i64; let mut w = 2i64; while i*i <= n { if n % i == 0 { return false; } i += w; w = 6 - w; } true } What would it take for me to be able to pass isize , i64 , usize , etc. as arguments? I have read through the Rust guide on the

How to write a trait bound for a reference to an associated type on the trait itself?

两盒软妹~` 提交于 2019-12-18 09:08:21
问题 I have this code: extern crate serde; use serde::de::DeserializeOwned; use serde::Serialize; trait Bar<'a, T: 'a> where T: Serialize, &'a T: DeserializeOwned, { } I would like to write this using an associated type, because the type T is unimportant to the users of this type. I got this far: trait Bar { type T: Serialize; } I cannot figure out how to specify the other bound. Ultimately, I want to use a function like this: extern crate serde_json; fn test<I: Bar>(t: I::T) -> String { serde