Rustのチュートリアルを終えたばかりです。rustc 1.34.0-nightly
下記のコードをコンパイルできません。
rust
1use pyo3::prelude::*; 2use pyo3::wrap_pyfunction; 3use pyo3::types::PyBytes; 4use sha2::{Sha256, Sha512, Digest}; 5use byteorder::{LittleEndian, WriteBytesExt}; 6use bigint::U512; 7 8#[pyfunction] 9fn sha256hash_vec(byte: &PyBytes) -> PyResult<Vec<u8>> { 10 let mut hasher = Sha256::new(); 11 hasher.input(byte.as_bytes()); 12 let r = hasher.result().to_vec(); 13 Ok(r) 14} 15 16#[pyfunction] 17fn sha256hash_bytes(_py: Python<'_>, byte: &PyBytes) -> PyResult<Py<PyBytes>> { 18 let mut hasher = Sha256::new(); 19 hasher.input(byte.as_bytes()); 20 let mut r = hasher.result(); 21 Ok(PyBytes::new(_py, &mut r)) 22} 23 24#[inline] 25fn int2byte(int: u32) ->Vec<u8> { 26 let mut wtr = Vec::with_capacity(4); 27 wtr.write_u32::<LittleEndian>(int).unwrap(); 28 wtr 29} 30 31#[inline] 32fn byte2int(byte: &[u8]) -> U512 { 33 U512::from(byte) 34} 35 36#[pyfunction] 37fn hash_generator(_py: Python<'_>, address: &str, nonce: u32) -> Vec<Py<PyBytes>> { 38 let mut hasher = Sha512::new(); 39 // seed [address 40bytes]-[nonce 4bytes] 40 hasher.input(address.as_bytes()); 41 hasher.input(int2byte(nonce)); 42 let mut hashed_ints = Vec::with_capacity(64); 43 for _ in 0..64 { 44 let hash = hasher.result(); 45 hashed_ints.push(byte2int(hash.as_slice())); 46 hasher.input(hash); 47 } 48 let hash = hasher.result(); 49 let final_int = byte2int(hash.as_slice()); 50 let mut hash_bytes = Vec::with_capacity(128); 51 for int in hashed_ints { 52 let int = int ^ final_int; 53 let mut bytes: [u8; 128]; 54 int.to_little_endian(&mut bytes); 55 hash_bytes.push(PyBytes::new(_py, &mut bytes)); 56 } 57 return hash_bytes; 58} 59 60/// This module is a python module implemented in Rust. 61#[pymodule] 62fn pochash(_py: Python, m: &PyModule) -> PyResult<()> { 63 //m.add_wrapped(wrap_pyfunction!(hash_generator))?; 64 Ok(()) 65}
error
1error[E0382]: use of moved value: `hasher` 2 --> src\lib.rs:44:20 3 | 438 | let mut hasher = Sha512::new(); 5 | ---------- move occurs because `hasher` has type `sha2::sha512::Sha512`, which does not implement the `Copy` trait 6... 744 | let hash = hasher.result(); 8 | ^^^^^^ value moved here, in previous iteration of loop 9 10error[E0381]: borrow of possibly uninitialized variable: `bytes` 11 --> src\lib.rs:54:30 12 | 1354 | int.to_little_endian(&mut bytes); 14 | ^^^^^^^^^^ use of possibly uninitialized `bytes` 15 16error: aborting due to 2 previous errors
以下の二点が疑問としてあがりました。
- hasher.result()の出力なのですが、GenericArrayの扱い方がよくわかりません。変数が束縛されたはずなのですがMoveされている?
- U512をBytesに変換したいのですがやり方がおかしいようです。to_little_endianを使用すると思われるのですがうまくいかず、正しいやり方を教えて下さい。documentより読み取れるようになると嬉しいのですが。
解答の方を宜しくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。