From 42398a541d168f1cb3e168b4af6f85a9f55cf120 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Blin?= <sebastien.blin@savoirfairelinux.com> Date: Tue, 29 Oct 2019 13:05:14 -0400 Subject: [PATCH] rust: infohash support == operator and from hex --- rust/src/ffi.rs | 2 ++ rust/src/infohash.rs | 9 +++++++++ rust/src/value.rs | 2 +- 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/rust/src/ffi.rs b/rust/src/ffi.rs index 035949d1..8cd2c418 100644 --- a/rust/src/ffi.rs +++ b/rust/src/ffi.rs @@ -44,6 +44,7 @@ pub struct Blob } #[repr(C)] +#[derive(PartialEq)] pub struct InfoHash { pub d: [u8; HASH_LEN], @@ -144,6 +145,7 @@ extern { pub fn dht_infohash_print(h: *const InfoHash) -> *const c_char; pub fn dht_infohash_random(h: *mut InfoHash); pub fn dht_infohash_get(h: *mut InfoHash, dat: *mut u8, dat_size: size_t); + pub fn dht_infohash_from_hex(h: *mut InfoHash, dat: *const c_char); pub fn dht_infohash_is_zero(j: *const InfoHash) -> bool; // dht::PkId diff --git a/rust/src/infohash.rs b/rust/src/infohash.rs index df11ccd2..a661bd36 100644 --- a/rust/src/infohash.rs +++ b/rust/src/infohash.rs @@ -55,6 +55,15 @@ impl InfoHash { h } + pub fn from_hex(data: &str) -> InfoHash { + let mut h = InfoHash::new(); + unsafe { + let c_str = CString::new(data).unwrap(); + dht_infohash_from_hex(&mut h, c_str.as_ptr()); + } + h + } + pub fn is_zero(&self) -> bool { unsafe { dht_infohash_is_zero(self) diff --git a/rust/src/value.rs b/rust/src/value.rs index 9bee6493..4794f15a 100644 --- a/rust/src/value.rs +++ b/rust/src/value.rs @@ -65,7 +65,7 @@ impl Value { } } - fn recipient(&self) -> InfoHash { + pub fn recipient(&self) -> InfoHash { unsafe { dht_value_get_recipient(self) } -- GitLab