From 2c0d43497d626e61a8de7a1efaff13d44e30d556 Mon Sep 17 00:00:00 2001 From: William Casarin Date: Tue, 2 Jan 2024 08:38:46 -0800 Subject: [PATCH] block: add as_str helper for nrelay --- src/block.rs | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/block.rs b/src/block.rs index c456f35..dee6e07 100644 --- a/src/block.rs +++ b/src/block.rs @@ -48,6 +48,23 @@ pub enum Mention<'a> { Addr(&'a bindings::bech32_naddr), } +impl bindings::ndb_str_block { + pub fn as_str(&self) -> &str { + unsafe { + let ptr = bindings::ndb_str_block_ptr(self as *const Self as *mut Self) as *const u8; + let len = bindings::ndb_str_block_len(self as *const Self as *mut Self); + let byte_slice = std::slice::from_raw_parts(ptr, len.try_into().unwrap()); + std::str::from_utf8_unchecked(byte_slice) + } + } +} + +impl bindings::bech32_nrelay { + pub fn as_str(&self) -> &str { + self.relay.as_str() + } +} + impl bindings::bech32_nprofile { pub fn pubkey(&self) -> &[u8; 32] { unsafe { &*(self.pubkey as *const [u8; 32]) } @@ -148,10 +165,8 @@ impl<'a> Block<'a> { if str_block.is_null() { return ""; } - let ptr = bindings::ndb_str_block_ptr(str_block) as *const u8; - let len = bindings::ndb_str_block_len(str_block); - let byte_slice = std::slice::from_raw_parts(ptr, len.try_into().unwrap()); - std::str::from_utf8_unchecked(byte_slice) + + (&*str_block).as_str() } }