2015-02-09 67 views
1

有什么办法,使此代码的工作,而无需使用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()) 
} 

它使用这个库https://github.com/DaGenix/rust-crypto

+3

我一定是盲目的,因为我没有看到任何'Box'有 – 2015-02-09 06:36:56

+2

请创建一个最小的编译例子 – 2015-02-09 07:44:23

+0

@JorgeIsraelPeña,没有盒子在那里。但Box可以用来使它工作。但我不想用它。 – 2015-02-09 08:00:19

回答

3

你需要或者盒或使用引用,作为“特质对象”可以只在指针后面工作。

下面是一个非常简化的代码版本。您必须实现相同的特质三种不同的结构(摘要)

struct Sha256; 
struct Sha384; 
struct Sha512; 

trait Digest {} 
impl Digest for Sha256 {} 
impl Digest for Sha384 {} 
impl Digest for Sha512 {} 

struct HMac<D: Digest> { d: D } 

fn main() { 
    let a = 1; 

    // what you're trying to do 
    // (does not work, Sha256, Sha384 and Sha512 are different types) 
    //let _ = match a { 
    // 1 => Sha256, 
    // 2 => Sha384, 
    // 3 => Sha512, 
    // _ => unreachable!() 
    //}; 
} 

注意的是,在现实情况下,不仅所有ShaXXX类型是类型系统不同,它们具有不同的内存布局以及(比较Engine256State例如Engine512State),所以这排除了不安全的技巧transmute

所以,说,你可以用盒子或引用(但你必须在比赛前预先创建一个具体的例子,如果你想使用的引用):

fn main() { 
    let a = 1; 

    let _ : Box<Digest> = match a { 
     1 => Box::new(Sha256), 
     2 => Box::new(Sha384), 
     3 => Box::new(Sha512), 
     _ => unreachable!() 
    }; 

    // to use references we need a pre-existing instance of all ShaXXX 
    let (sha256, sha384, sha512) = (Sha256, Sha384, Sha512); 

    let _ : &Digest = match a { 
     1 => &sha256, //... otherwise the reference wouldn't outlive the match 
     2 => &sha384, 
     3 => &sha512, 
     _ => unreachable!() 
    }; 
} 

。请注意,Box做相当于大多数垃圾收集语言在您想通过其界面使用对象时为您提供的内容。有些内存是为具体对象动态分配的,但只能真正允许传递一个指向内存的指针。

在你的情况(但我没有测试过下面的代码),你应该能够做到:

//HMac implements a Mac trait, so we can return a Box<Mac> 
// (I'm assuming you only want to use HMac through its Mac trait) 
fn create_hmac<'a, D: Digest>(digest: D, some_str: &'a str) -> Box<Mac + 'a> { 
    Box::new(Hmac::new(digest, some_str.to_string().as_bytes())) 
} 

,并用它作为:

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!() 
    }; 
+0

根据您的解决方案,我将如何创建Hmac'Hmac :: new(???,some_str.to_string()。as_bytes())'? – 2015-02-09 11:38:16

+0

什么是'a',为什么它等于1? – 2015-02-09 11:41:09

+0

@AlexanderSupertramp'a'只代表你的'MyType'枚举,以提供一个可编辑的例子。 – 2015-02-09 11:44:16

3

一次加法和一次澄清保罗的好回答。首先,你可以让你的枚举包含合适的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() 
} 

而且,你不必实际实例的类型所有如果你想使用参考资料,你只需要确保你使用的是可用的。你也必须有地方参考可以住超过match表达:

#![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!() 
    }; 
} 
+0

'let mut hmac = Hmac :: new(* digest,“fdsfds”.to_string()。as_bytes());'===> 1) 2)'error:type crypto :: hmac :: Hmac 不会实现任何类型的密码。方法在名为input的作用域中 – 2015-02-10 14:07:17