traits

How to move a Vec<Box<dyn Trait>> Into Vec<Rc<RefCell<dyn Trait>>>

浪子不回头ぞ 提交于 2021-02-20 10:39:12
问题 I have a Vec<Box<dyn Trait>> as input, and I want to store its elements in a Vec<Rc<RefCell<dyn Trait>>> . What is the best way to do it? I tried with: use std::cell::RefCell; use std::rc::Rc; trait Trait {} fn main() { let mut source: Vec<Box<dyn Trait>> = Vec::new(); let mut dest: Vec<Rc<RefCell<dyn Trait>>> = Vec::new(); for s in source { let d = Rc::new(RefCell::new(s.as_ref())); dest.push(d); } } But I got the error: error[E0277]: the trait bound `&dyn Trait: Trait` is not satisfied -->

Is there a way to have a public trait in a proc-macro crate?

北战南征 提交于 2021-02-16 21:20:47
问题 I have a proc-macro crate with a macro that, when expanded, needs to use custom trait implementations for Rust built-in types. I tried to define the trait in the same crate, but Rust tells me that a proc-macro crate can only have public macros (the functions annotated with #[proc_macro] ) and nothing else can be public. So I put the trait in another crate and in the proc-macro crate included it as a dependency. But this means that anyone that wants to use my proc-macro crate has to depend on

Is there a way to have a public trait in a proc-macro crate?

孤街醉人 提交于 2021-02-16 21:20:08
问题 I have a proc-macro crate with a macro that, when expanded, needs to use custom trait implementations for Rust built-in types. I tried to define the trait in the same crate, but Rust tells me that a proc-macro crate can only have public macros (the functions annotated with #[proc_macro] ) and nothing else can be public. So I put the trait in another crate and in the proc-macro crate included it as a dependency. But this means that anyone that wants to use my proc-macro crate has to depend on

How to return concrete type from generic function?

天大地大妈咪最大 提交于 2021-02-16 15:29:06
问题 In the example below the Default trait is used just for demonstration purposes. My questions are: What is the difference between the declarations of f() and g() ? Why g() doesn't compile since it's identical to f() ? How can I return a concrete type out of a impl trait generically typed declaration? struct Something { } impl Default for Something { fn default() -> Self { Something{} } } // This compiles. pub fn f() -> impl Default { Something{} } // This doesn't. pub fn g<T: Default>() -> T {

Typedef in traits vs typedef in class

本小妞迷上赌 提交于 2021-02-15 11:45:39
问题 I'm looking through the Eigen source code for educational purposes. I've noticed that for each concrete class template X in the hierarchy, there is an internal::traits<X> defined. A typical example can be found in Matrix.h: namespace internal { template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols> struct traits<Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> > { typedef _Scalar Scalar; typedef Dense StorageKind; typedef DenseIndex Index;

Pattern binding the same variable to different types sharing a trait

a 夏天 提交于 2021-02-11 14:49:10
问题 I have a question about pattern matching on values sharing some behaviour through a trait. I have an enum with two variants, each binding value of different types, where both types implement a trait. I'm trying to figure out whether it's possible to create a single pattern (of the E::VarA(x) | E::VarB(x) form) in which I bind both types to a single constant, provided I'm only interested in using the shared behaviour. An illustrative example: Playground: trait T { fn f(&self) -> usize; }

&[u8] cannot be indexed by RangeFull?

北城以北 提交于 2021-02-11 14:20:00
问题 I am trying to create a struct which can contain anything that is sliceable to [u8] . use std::ops::{Index, RangeFull}; struct Wrapper<DataT> where DataT: ?Sized + Index<RangeFull, Output = [u8]>, { data: DataT, } impl<DataT> Wrapper<DataT> where DataT: ?Sized + Index<RangeFull, Output = [u8]>, { fn subwrapper(&self) -> Wrapper<Vec<u8>> { let mut buffer = Vec::<u8>::new(); buffer.extend_from_slice(&self.data[..]); Wrapper { data: buffer, } } } fn main() { let data : [u8; 3] = [1, 2, 3]; let w

How can I use serde to deserialize into a hierarchical decentralized configuration? [duplicate]

谁说我不能喝 提交于 2021-02-10 23:26:52
问题 This question already has answers here : How can deserialization of polymorphic trait objects be added in Rust if at all? (3 answers) How do I deserialize into trait, not a concrete type? (3 answers) Closed 6 months ago . I want to have a JSON config file of the form: { "general_option_foo": true, "general_option_bar": "hello world!", "modules": [ { "name": "moduleA", "module_options" : {"optFoo":"foo"} }, { "name" : "moduleB", "module_options" : {"optBar":[3,4], "optBuzz": true} } { "name":

Why are len() and is_empty() not defined in a trait?

女生的网名这么多〃 提交于 2021-02-10 18:34:16
问题 Most patterns in Rust are captured by traits ( Iterator , From , Borrow , etc.). How come a pattern as pervasive as len / is_empty has no associated trait in the standard library? Would that cause problems which I do not foresee? Was it deemed useless? Or is it only that nobody thought of it (which seems unlikely)? 回答1: Was it deemed useless? I would guess that's the reason. What could you do with the knowledge that something is empty or has length 15? Pretty much nothing, unless you also

How do I specify the expected result of a `std::ops::Mul` in a trait bound?

守給你的承諾、 提交于 2021-02-10 18:01:35
问题 I have: use std::ops::{Add, Div, Mul, Neg, Sub}; pub trait Hilbert: Add + Sub + Mul + Div + Neg + Mul<f64> + Div<f64> + Sized { fn dot(&self, other: &Self) -> f64; fn magnitude(&self) -> f64; } fn g<T: Hilbert>(x: T) -> f64 { return (x * 2.0).dot(x); } ...which yields: error[E0599]: no method named `dot` found for type `<T as std::ops::Mul<f64>>::Output` in the current scope --> src/main.rs:9:22 | 9 | return (x * 2.0).dot(x); | ^^^ | = help: items from traits can only be used if the trait is