-1

type であると想定するフィールドを持つ構造体を作成しようとしていますResult<TempDir>。でフィールドの実装を初期化するときにnew()、新しい一時ディレクトリを作成してその特定のフィールドを初期化したいと考えています。後で、そのディレクトリから読み取るメソッドを実装したいと思います。

これがコードです。ロジックが正しいはずなので、構文とライブラリの適切な使用についてもっと心配しています(Rustで読み取り/書き込みバッファリング用のライブラリが4つ以上あるのはなぜですか、これは正気ではありません)。トレイトの実装についてはあまり心配しないでください。必要なのは構文の指示だけです。あまり厳しくしないでください。コンパイルできないことはわかっていますが、2 つの変更だけでコンパイルできるはずです。

    extern crate rustc_back;
    use std::path::Path;
    use std::fs::File;
    use rustc_back::tempdir::TempDir as TempDir;
    pub struct MyStorage {

      temp_dir : Result<TempDir>
    }

    impl MyStorage {
      pub fn new() -> MyStorage { 
        //tempo = match TempDir::new("encrypt_storage");
        let store = match TempDir::new("encrypt_storage") {
          Ok(dir) => dir,
            Err(e) => panic!("couldn't create temporary directory: {}", e)
        };

        MyStorage { temp_dir: store }
        //MyStorage { temp_dir: TempDir::new("encrypt_storage") }
      }
    }

    impl Storage for MyStorage {
      fn get(&self, name: Vec<u8>) -> Vec<u8> {
    //let mut f = std::fs::File::open(self.temp_dir.path() / name);
    let mut f = std::fs::File::open(&self.temp_dir){
        // The `desc` field of `IoError` is a string that describes the error
        Err(why) => panic!("couldn't open: {}", why.description()),
        Ok(file) => file,
    };
    let mut s = String::new();
    //f.read_to_string(&mut s);
    match f.read_to_string(&mut s){
        Err(why) => panic!("couldn't read: {}", why.description()),
        Ok(_) => print!("contains:\n{}", s),
    }
    s.to_vec()
  }

  fn put(&mut self, name: Vec<u8>, data: Vec<u8>) {
    // self.entries.push(Entry { name : name, data : data })
    let mut f = File::create(self.temp_dir.path() / name);
    f.write_all(data);
  }

      fn put(&mut self, name: Vec<u8>, data: Vec<u8>) {
        // self.entries.push(Entry { name : name, data : data })
        let mut f = File::create(self.temp_dir.path() / name);
        f.write_all(data);
      }
    }
4

1 に答える 1