traits

How to coerce a Vec of structs to a Vec of trait objects?

五迷三道 提交于 2021-02-05 07:21:29
问题 Trying to create a DB struct that is a HashMap of vectors. Each Vec contains Box<dyn Model> . use std::collections::HashMap; trait Model { fn id(&self) -> i32; } struct User; struct Message; impl Model for User { fn id(&self) -> i32 { 4 } } impl Model for Message { fn id(&self) -> i32 { 3 } } struct DB { users: Vec<Box<User>>, messages: Vec<Box<Message>>, tables: HashMap<String, Vec<Box<dyn Model>>>, } impl DB { fn new() -> Self { let users: Vec<Box<User>> = Vec::new(); let messages: Vec<Box

How to coerce a Vec of structs to a Vec of trait objects?

元气小坏坏 提交于 2021-02-05 07:21:06
问题 Trying to create a DB struct that is a HashMap of vectors. Each Vec contains Box<dyn Model> . use std::collections::HashMap; trait Model { fn id(&self) -> i32; } struct User; struct Message; impl Model for User { fn id(&self) -> i32 { 4 } } impl Model for Message { fn id(&self) -> i32 { 3 } } struct DB { users: Vec<Box<User>>, messages: Vec<Box<Message>>, tables: HashMap<String, Vec<Box<dyn Model>>>, } impl DB { fn new() -> Self { let users: Vec<Box<User>> = Vec::new(); let messages: Vec<Box

How can I better store a string to avoid many clones?

孤街浪徒 提交于 2021-02-05 06:31:21
问题 I am using tokio's UdpCodec trait: pub trait UdpCodec { type In; type Out; fn decode(&mut self, src: &SocketAddr, buf: &[u8]) -> Result<Self::In>; fn encode(&mut self, msg: Self::Out, buf: &mut Vec<u8>) -> SocketAddr; } My associated type for In is a (SocketAddr, Vec<Metric>) . Metric is defined as: #[derive(Debug, PartialEq)] pub struct Metric { pub name: String, pub value: f64, pub metric_type: MetricType, pub sample_rate: Option<f64>, } I have used owned strings to avoid lifetime

How can I better store a string to avoid many clones?

末鹿安然 提交于 2021-02-05 06:31:08
问题 I am using tokio's UdpCodec trait: pub trait UdpCodec { type In; type Out; fn decode(&mut self, src: &SocketAddr, buf: &[u8]) -> Result<Self::In>; fn encode(&mut self, msg: Self::Out, buf: &mut Vec<u8>) -> SocketAddr; } My associated type for In is a (SocketAddr, Vec<Metric>) . Metric is defined as: #[derive(Debug, PartialEq)] pub struct Metric { pub name: String, pub value: f64, pub metric_type: MetricType, pub sample_rate: Option<f64>, } I have used owned strings to avoid lifetime

Rust equivalent to Swift's extension methods to a protocol?

人走茶凉 提交于 2021-02-05 06:30:45
问题 In Swift I can attach extension methods to any of struct , enum or protocol (same with trait in Rust). protocol Foo1 { func method1() -> Int } extension Foo1 { func method2() { print("\(method1())") } } Then all types conforming the protocol Foo1 now all have method2() . This is very useful to build "method chaining" easily. How to do same in Rust? This doesn't work with an error. struct Kaz {} impl Foo for Kaz {} trait Foo { fn sample1(&self) -> isize { 111 } } impl Foo { fn sample2(&self) {

Clone an Rc<RefCell<MyType> trait object and cast it

杀马特。学长 韩版系。学妹 提交于 2021-02-04 16:42:18
问题 This question is related to Rust: Clone and Cast Rc pointer Let's say I have this piece of code which works fine: use std::rc::Rc; trait TraitAB : TraitA + TraitB { fn as_a(self: Rc<Self>) -> Rc<dyn TraitA>; fn as_b(self: Rc<Self>) -> Rc<dyn TraitB>; } trait TraitA {} trait TraitB {} struct MyType {} impl TraitAB for MyType { fn as_a(self: Rc<Self>) -> Rc<dyn TraitA> {self} fn as_b(self: Rc<Self>) -> Rc<dyn TraitB> {self} } impl TraitA for MyType {} impl TraitB for MyType {} fn main() { let a

Clone an Rc<RefCell<MyType> trait object and cast it

感情迁移 提交于 2021-02-04 16:42:12
问题 This question is related to Rust: Clone and Cast Rc pointer Let's say I have this piece of code which works fine: use std::rc::Rc; trait TraitAB : TraitA + TraitB { fn as_a(self: Rc<Self>) -> Rc<dyn TraitA>; fn as_b(self: Rc<Self>) -> Rc<dyn TraitB>; } trait TraitA {} trait TraitB {} struct MyType {} impl TraitAB for MyType { fn as_a(self: Rc<Self>) -> Rc<dyn TraitA> {self} fn as_b(self: Rc<Self>) -> Rc<dyn TraitB> {self} } impl TraitA for MyType {} impl TraitB for MyType {} fn main() { let a

Clone an Rc<RefCell<MyType> trait object and cast it

喜欢而已 提交于 2021-02-04 16:42:05
问题 This question is related to Rust: Clone and Cast Rc pointer Let's say I have this piece of code which works fine: use std::rc::Rc; trait TraitAB : TraitA + TraitB { fn as_a(self: Rc<Self>) -> Rc<dyn TraitA>; fn as_b(self: Rc<Self>) -> Rc<dyn TraitB>; } trait TraitA {} trait TraitB {} struct MyType {} impl TraitAB for MyType { fn as_a(self: Rc<Self>) -> Rc<dyn TraitA> {self} fn as_b(self: Rc<Self>) -> Rc<dyn TraitB> {self} } impl TraitA for MyType {} impl TraitB for MyType {} fn main() { let a

The proper ownership for “caching proxy” in Rust?

瘦欲@ 提交于 2021-01-29 20:00:58
问题 I'd like to use Factory to build an object from the String and have multiple impls: 1) actual building and 2) caching (stores in-memory in HashMap ). The problem is that in case #1 it have to pass the ownership and in case #2 HashMap owns the value and a reference can be returned only. use std::collections::HashMap; // product interface pub trait TProduct { fn get_title(&self) -> &String; } // and concrete impls pub struct ConcreteProduct1 { } impl TProduct for ConcreteProduct1 { // ... } pub

The proper ownership for “caching proxy” in Rust?

不羁的心 提交于 2021-01-29 15:48:25
问题 I'd like to use Factory to build an object from the String and have multiple impls: 1) actual building and 2) caching (stores in-memory in HashMap ). The problem is that in case #1 it have to pass the ownership and in case #2 HashMap owns the value and a reference can be returned only. use std::collections::HashMap; // product interface pub trait TProduct { fn get_title(&self) -> &String; } // and concrete impls pub struct ConcreteProduct1 { } impl TProduct for ConcreteProduct1 { // ... } pub