Rust 我一定要在这里装箱吗?
是否有任何方法可以在不使用Rust 我一定要在这里装箱吗?,rust,Rust,是否有任何方法可以在不使用Boxing的情况下使此代码正常工作: fn some_func(my_type: MyType, some_str: &str) -> bool { let mut hmac = match my_type { MyType::MyType1 => create_hmac(Sha256::new(), some_str), MyType::MyType2 => create_hmac(Sha384::new(), some
Box
ing的情况下使此代码正常工作:
fn some_func(my_type: MyType, some_str: &str) -> bool {
let mut hmac = match my_type {
MyType::MyType1 => create_hmac(Sha256::new(), some_str),
MyType::MyType2 => create_hmac(Sha384::new(), some_str),
MyType::MyType3 => create_hmac(Sha512::new(), some_str),
_ => panic!()
};
//some calculations goes HERE, NOT in create_hmac function...
hmac.input("fdsfdsfdsfd".to_string().as_bytes());
//something else....
true
}
fn create_hmac<D: Digest>(digest: D, some_str: &str) -> Hmac<D> {
Hmac::new(digest, some_str.to_string().as_bytes())
}
fn一些函数(我的类型:我的类型,一些类型:&str)->bool{
让mut hmac=匹配我的类型{
MyType::MyType1=>create_hmac(Sha256::new(),some_str),
MyType::MyType2=>create_hmac(Sha384::new(),some_str),
MyType::MyType3=>create_hmac(Sha512::new(),some_str),
_=>恐慌!()
};
//有些计算是在这里进行的,而不是在create_hmac函数中。。。
输入(“fdsfdsfdsfd.to_string()。as_bytes());
//还有别的。。。。
真的
}
fn创建hmac(摘要:D,一些摘要:&str)->hmac{
Hmac::new(摘要,一些字符串到字符串()作为字节())
}
它使用的库是您需要使用框或引用,因为“特征对象”只能在指针后面工作 这是一个非常简化的代码版本。您有三个不同的结构实现相同的特性(摘要) 并将其用作:
let mut hmac: Box<Mac> = match my_type {
MyType::MyType1 => create_hmac(Sha256::new(), some_str),
MyType::MyType2 => create_hmac(Sha384::new(), some_str),
MyType::MyType3 => create_hmac(Sha512::new(), some_str),
_ => unreachable!()
};
让mut hmac:Box=匹配我的类型{
MyType::MyType1=>create_hmac(Sha256::new(),some_str),
MyType::MyType2=>create_hmac(Sha384::new(),some_str),
MyType::MyType3=>create_hmac(Sha512::new(),some_str),
_=>无法访问!()
};
您需要框选或使用引用,因为“特征对象”只能在指针后面工作
这是一个非常简化的代码版本。您有三个不同的结构实现相同的特性(摘要)
并将其用作:
let mut hmac: Box<Mac> = match my_type {
MyType::MyType1 => create_hmac(Sha256::new(), some_str),
MyType::MyType2 => create_hmac(Sha384::new(), some_str),
MyType::MyType3 => create_hmac(Sha512::new(), some_str),
_ => unreachable!()
};
让mut hmac:Box=匹配我的类型{
MyType::MyType1=>create_hmac(Sha256::new(),some_str),
MyType::MyType2=>create_hmac(Sha384::new(),some_str),
MyType::MyType3=>create_hmac(Sha512::new(),some_str),
_=>无法访问!()
};
对保罗的好答案进行一次补充和一次澄清。首先,您可以使您的enum包含适当的Sha*
struct,然后通过适当的委托来实现Digest
。这可能在所有情况下都没有意义,但如果从概念上讲这就是您正在做的事情,那么这可能是有意义的:
struct Sha256;
struct Sha384;
struct Sha512;
trait Digest { fn digest(&self); }
impl Digest for Sha256 { fn digest(&self) {println!("256")} }
impl Digest for Sha384 { fn digest(&self) {println!("384")} }
impl Digest for Sha512 { fn digest(&self) {println!("512")} }
enum MyType {
One(Sha256),
Two(Sha384),
Three(Sha512),
}
impl Digest for MyType {
fn digest(&self) {
use MyType::*;
match *self {
One(ref sha) => sha.digest(),
Two(ref sha) => sha.digest(),
Three(ref sha) => sha.digest(),
}
}
}
fn main() {
let a = MyType::Two(Sha384);
a.digest()
}
此外,如果要使用引用,不必实际实例化所有类型,只需确保使用的类型可用即可。您还必须具有引用可以位于匹配
表达式之外的位置:
#![feature(std_misc)]
#![feature(io)]
use std::time::duration::Duration;
use std::old_io::timer::sleep;
struct Sha256(u8);
struct Sha384(u8);
struct Sha512(u8);
impl Sha256 { fn new() -> Sha256 { sleep(Duration::seconds(1)); Sha256(1) }}
impl Sha384 { fn new() -> Sha384 { sleep(Duration::seconds(2)); Sha384(2) }}
impl Sha512 { fn new() -> Sha512 { sleep(Duration::seconds(3)); Sha512(3) }}
trait Digest {}
impl Digest for Sha256 {}
impl Digest for Sha384 {}
impl Digest for Sha512 {}
fn main() {
let a = 1;
let sha256: Sha256;
let sha384: Sha384;
let sha512: Sha512;
let _ : &Digest = match a {
1 => {
sha256 = Sha256::new();
&sha256
},
2 => {
sha384 = Sha384::new();
&sha384
},
3 => {
sha512 = Sha512::new();
&sha512
},
_ => unreachable!()
};
}
对保罗的回答进行了一次补充和一次澄清。首先,您可以使您的enum包含适当的
Sha*
struct,然后通过适当的委托来实现Digest
。这可能在所有情况下都没有意义,但如果从概念上讲这就是您正在做的事情,那么这可能是有意义的:
struct Sha256;
struct Sha384;
struct Sha512;
trait Digest { fn digest(&self); }
impl Digest for Sha256 { fn digest(&self) {println!("256")} }
impl Digest for Sha384 { fn digest(&self) {println!("384")} }
impl Digest for Sha512 { fn digest(&self) {println!("512")} }
enum MyType {
One(Sha256),
Two(Sha384),
Three(Sha512),
}
impl Digest for MyType {
fn digest(&self) {
use MyType::*;
match *self {
One(ref sha) => sha.digest(),
Two(ref sha) => sha.digest(),
Three(ref sha) => sha.digest(),
}
}
}
fn main() {
let a = MyType::Two(Sha384);
a.digest()
}
此外,如果要使用引用,不必实际实例化所有类型,只需确保使用的类型可用即可。您还必须具有引用可以位于匹配
表达式之外的位置:
#![feature(std_misc)]
#![feature(io)]
use std::time::duration::Duration;
use std::old_io::timer::sleep;
struct Sha256(u8);
struct Sha384(u8);
struct Sha512(u8);
impl Sha256 { fn new() -> Sha256 { sleep(Duration::seconds(1)); Sha256(1) }}
impl Sha384 { fn new() -> Sha384 { sleep(Duration::seconds(2)); Sha384(2) }}
impl Sha512 { fn new() -> Sha512 { sleep(Duration::seconds(3)); Sha512(3) }}
trait Digest {}
impl Digest for Sha256 {}
impl Digest for Sha384 {}
impl Digest for Sha512 {}
fn main() {
let a = 1;
let sha256: Sha256;
let sha384: Sha384;
let sha512: Sha512;
let _ : &Digest = match a {
1 => {
sha256 = Sha256::new();
&sha256
},
2 => {
sha384 = Sha384::new();
&sha384
},
3 => {
sha512 = Sha512::new();
&sha512
},
_ => unreachable!()
};
}
我一定是瞎了,因为我没有看到任何
框
,请创建一个最小的可编译example@JorgeIsraelPe尼娜,里面没有盒子。但盒子可以用来让它工作。但我不想用它来解决你的问题。我可以复制粘贴到一个.rs文件并编译。我必须是盲的,因为我看不到任何框
,请创建一个最小的可编译文件example@JorgeIsraelPe尼娜,里面没有盒子。但盒子可以用来让它工作。但我不想用它来解决你的问题。一些我可以复制粘贴到一个.rs文件并编译的东西。我如何创建HmacHmac::new(???,一些字符串()作为字节())
根据你的解决方案?什么是a
,为什么它等于1?@AlexanderSupertrampa
只是代表你的MyType
枚举,提供一个可编译的示例。@AlexanderSupertramp我添加了一个更改示例,这些更改将使您的代码可编译,但我尚未测试它们。谢谢。关于这两种方法,我将如何创建Hmac::new,a是什么,为什么它等于1?特别是涉及&Digest
。根据您的解决方案,我如何创建HmacHmac::new(???,一些字符串()作为字节())
?什么是a
,为什么它等于1?@AlexanderSupertrampa
只是代表您的MyType
枚举,提供一个可编译的示例。@AlexanderSupertramp我添加了一个更改示例,这些更改将使您的代码可编译,但我尚未测试它们。谢谢。关于这两种方法,我将如何创建Hmac::new,a是什么,为什么它等于1?特别是涉及&Digest
的部分,让mut hmac=hmac::new(*Digest,“fdsfds.”to_string().as_bytes())
=>1)没有为类型crypto::digest::digest实现trait core::marker::Size
2)错误:类型crypto::hmac::hmac在名为input的作用域中未实现任何方法让mut hmac=hmac::new(*digest,“fdsfds.”to_string().as_bytes())
=>1)没有为类型crypto::digest::digest实现trait core::marker::Size
2)错误:类型crypto::hmac::hmac在名为input的范围内未实现任何方法