nostrdb-rs

nostrdb in rust!
git clone git://jb55.com/nostrdb-rs
Log | Files | Refs | Submodules | README | LICENSE

commit 2c0d43497d626e61a8de7a1efaff13d44e30d556
parent 351906121f79d71b2a5a999944a8a22c6b8dde4e
Author: William Casarin <jb55@jb55.com>
Date:   Tue,  2 Jan 2024 08:38:46 -0800

block: add as_str helper for nrelay

Diffstat:
Msrc/block.rs | 23+++++++++++++++++++----
1 file changed, 19 insertions(+), 4 deletions(-)

diff --git 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() } }