Skip to content

Commit

Permalink
block: add as_str helper for nrelay
Browse files Browse the repository at this point in the history
  • Loading branch information
jb55 committed Jan 2, 2024
1 parent 3519061 commit 2c0d434
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]) }
Expand Down Expand Up @@ -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()
}
}

Expand Down

0 comments on commit 2c0d434

Please sign in to comment.