如何获得Box<B>或&B或&Box<B>从a在此代码变量:
trait A {}
struct B;
impl A for B {}
fn main() {
let mut a: Box<dyn A> = Box::new(B);
let b = a as Box<B>;
}
Run Code Online (Sandbox Code Playgroud)
此代码返回错误:
error[E0605]: non-primitive cast: `std::boxed::Box<dyn A>` as `std::boxed::Box<B>`
--> src/main.rs:8:13
|
8 | let b = a as Box<B>;
| ^^^^^^^^^^^
|
= note: an `as` expression can only be used to convert between primitive types. Consider using the `From` trait
Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个泛型结构,它使用"整数类型"来引用数组.出于性能原因,我希望能够轻松指定是否使用u16,u32或u64.像这样的东西(显然是无效的Rust代码):
struct Foo<T: u16 or u32 or u64> { ... }
Run Code Online (Sandbox Code Playgroud)
有没有办法表达这个?
我有几种实现特征(关系)的类型。我需要在它们之间传递数据,比如INSERT INTO FROM SELECT来自 sql。
但是,有时我会移动来自相同类型的数据,这意味着我可以使用更直接的方式:
impl Relation for BTree {
fn new_from<R: Relation>(names: Schema, of: R) -> Self {
if of is Btree { //How do this
//Fast path
cast(of as Btree).clone() //And this
} else {
//Generic path
}
}
}
Run Code Online (Sandbox Code Playgroud) 我认为如果我能算出生命周期符号,这可能会起作用,我错了吗?
pub fn from<T:Pattern>(from: T) -> Result<Tag, &'static str> {
match from {
'A'|"A" => Ok(Tag::ChA),
'B'|"B" => Ok(Tag::ChB),
'C'|"C" => Ok(Tag::ChC),
'D'|"D" => Ok(Tag::ChD),
'T'|"Tmpr" => Ok(Tag::Tmpr),
'Y'|"Batt" => Ok(Tag::Batt),
'L'|"Acc" => Ok(Tag::Acc),
_ => Err("Error: unknown channel"),
}
}
Run Code Online (Sandbox Code Playgroud)