traits

Why isnt Send implemented for a struct containing Arc?

こ雲淡風輕ζ 提交于 2020-08-10 20:13:09
问题 I'm using a crate to interact with Postgres with simply writing sql queries by hands (Diesel seems for my simple case) and got stuck about the multithreaded access to the database client. Here is the code: use postgres::Client; pub struct Database{ connection: Arc<Client> } impl Database { pub fn from_config(url: &str) -> Database { //... } } fn main() { let url: String = //... let db = db::Database::from_config(&url); let db_ref = Arc::new(db); consume(future(Arc::clone(&db_ref))); // <-----

How to combine F-bounded polymorphism with associated types in Scala?

牧云@^-^@ 提交于 2020-06-01 05:49:11
问题 I have a trait called Graphlike for things that work as a graph. Notably, one of the properties I want to have is that the method g.subgraph(Set(1, 2, 3)) would return a subgraph of the same type with just the vertices 1, 2 and 3. Apparently, this means that I want F-bounded polymorphism and that Graphlike looks something like this: trait Graphlike[A <: Graphlike[A]] { type Vertex def subgraph(selectedVertices: Set[Vertex]): A } I also have a trait representing an automaton with associated

In Rust, what's the idiomatic way to split a &str into an iterator of &strs of one character each?

烂漫一生 提交于 2020-04-13 14:52:16
问题 If I want to take a &str like "aeiou" and turn it into an iterator roughly equivalent to ["a", "e", "i", "o", "u"].iter() , what's the most idiomatic way to do it? I've tried doing "aeiou".split("") which seemed idiomatic to me, but I got empty &str s at the beginning and end. I've tried doing "aeiou".chars() but it got pretty ugly and unwieldy from there trying to turn the char s into &str s. For the time being, I just typed out ["a", "e", "i", "o", "u"].iter() , but there's got to be an

In Rust, what's the idiomatic way to split a &str into an iterator of &strs of one character each?

落爺英雄遲暮 提交于 2020-04-13 14:51:09
问题 If I want to take a &str like "aeiou" and turn it into an iterator roughly equivalent to ["a", "e", "i", "o", "u"].iter() , what's the most idiomatic way to do it? I've tried doing "aeiou".split("") which seemed idiomatic to me, but I got empty &str s at the beginning and end. I've tried doing "aeiou".chars() but it got pretty ugly and unwieldy from there trying to turn the char s into &str s. For the time being, I just typed out ["a", "e", "i", "o", "u"].iter() , but there's got to be an

What does a trait requiring Sized have to do with being unable to have trait objects of that trait?

好久不见. 提交于 2020-04-13 03:54:11
问题 I have this code (playground): trait NodeLike: Sized {} fn main() { let s: Box<NodeLike> = panic!(); } Which does not compile: error[E0038]: the trait `NodeLike` cannot be made into an object --> src/main.rs:4:12 | 4 | let s: Box<NodeLike> = panic!(); | ^^^^^^^^^^^^^ the trait `NodeLike` cannot be made into an object | = note: the trait cannot require that `Self : Sized` After all I read, I still don't understand why it does not compile and why it does without the Sized constraint. As far as

How to delegate an async function with non-static parameter by a trait?

牧云@^-^@ 提交于 2020-03-23 07:55:08
问题 Like this code: use std::future::Future; use std::pin::Pin; trait A { fn handle<'a>(&'a self, data: &'a i32) -> Pin<Box<dyn 'a + Future<Output = ()>>>; } impl<'b, Fut> A for fn(&'b i32) -> Fut where Fut: 'b + Future<Output = ()>, { fn handle<'a>(&'a self, data: &'a i32) -> Pin<Box<dyn 'a + Future<Output = ()>>> { Box::pin(self(data)) } } how can I implement A for all async fn(&i32) ? 回答1: This code should works: use std::future::Future; use std::pin::Pin; trait A<'a> { fn handle(&'a self,

Trait runtime type of type parameter through TypeTag when used with Existential type in Scala

送分小仙女□ 提交于 2020-03-20 12:01:07
问题 I have trait with type parameter. To get the runtime type I use TypeTag . However, when this trait (and its classes) are used with existential type in a Collection, e.g. List or Map , TypeTag is "lost". Here is an example of standard way to use Type Tag: scala> import scala.reflect.runtime.universe._ import scala.reflect.runtime.universe._ scala> trait Animal[T] { | def typeT()(implicit t: TypeTag[T]) = t.tpe | } defined trait Animal scala> scala> class Dog extends Animal[Int] defined class

Trait runtime type of type parameter through TypeTag when used with Existential type in Scala

十年热恋 提交于 2020-03-20 11:57:19
问题 I have trait with type parameter. To get the runtime type I use TypeTag . However, when this trait (and its classes) are used with existential type in a Collection, e.g. List or Map , TypeTag is "lost". Here is an example of standard way to use Type Tag: scala> import scala.reflect.runtime.universe._ import scala.reflect.runtime.universe._ scala> trait Animal[T] { | def typeT()(implicit t: TypeTag[T]) = t.tpe | } defined trait Animal scala> scala> class Dog extends Animal[Int] defined class