nostrdb-rs

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

commit 5733ece62b8495db8624a21637bacd12ebb22b2c
parent c95d34cb77815deeadee4cc453b24b948c479569
Author: William Casarin <jb55@jb55.com>
Date:   Fri, 24 May 2024 12:56:13 -0700

fix tag borrowing lifetimes

simply copy around Notes which are just pointers anyways

Signed-off-by: William Casarin <jb55@jb55.com>

Diffstat:
Msrc/ndb_str.rs | 8++++----
Msrc/note.rs | 4++--
Msrc/tags.rs | 45++++++++++++++++++++++++---------------------
3 files changed, 30 insertions(+), 27 deletions(-)

diff --git a/src/ndb_str.rs b/src/ndb_str.rs @@ -2,7 +2,7 @@ use crate::{bindings, Note}; pub struct NdbStr<'a> { ndb_str: bindings::ndb_str, - note: &'a Note<'a>, + note: Note<'a>, } #[derive(Debug, Copy, Clone, PartialEq, Eq)] @@ -38,11 +38,11 @@ impl bindings::ndb_str { } impl<'a> NdbStr<'a> { - pub fn note(&self) -> &'a Note<'a> { - self.note + pub fn note(&self) -> &Note<'a> { + &self.note } - pub(crate) fn new(ndb_str: bindings::ndb_str, note: &'a Note<'a>) -> Self { + pub(crate) fn new(ndb_str: bindings::ndb_str, note: Note<'a>) -> Self { NdbStr { ndb_str, note } } diff --git a/src/note.rs b/src/note.rs @@ -137,9 +137,9 @@ impl<'a> Note<'a> { unsafe { bindings::ndb_note_kind(self.as_ptr()) } } - pub fn tags(&'a self) -> Tags<'a> { + pub fn tags(&self) -> Tags<'a> { let tags = unsafe { bindings::ndb_note_tags(self.as_ptr()) }; - Tags::new(tags, self) + Tags::new(tags, self.clone()) } pub fn sig(&self) -> &'a [u8; 64] { diff --git a/src/tags.rs b/src/tags.rs @@ -1,13 +1,13 @@ use crate::{bindings, NdbStr, Note}; -#[derive(Debug, Copy, Clone)] +#[derive(Debug, Clone)] pub struct Tag<'a> { ptr: *mut bindings::ndb_tag, - note: &'a Note<'a>, + note: Note<'a>, } impl<'a> Tag<'a> { - pub(crate) fn new(ptr: *mut bindings::ndb_tag, note: &'a Note<'a>) -> Self { + pub(crate) fn new(ptr: *mut bindings::ndb_tag, note: Note<'a>) -> Self { Tag { ptr, note } } @@ -23,7 +23,7 @@ impl<'a> Tag<'a> { ind as ::std::os::raw::c_int, ) }; - NdbStr::new(nstr, self.note) + NdbStr::new(nstr, self.note.clone()) } pub fn get(&self, ind: u16) -> Option<NdbStr<'a>> { @@ -33,8 +33,8 @@ impl<'a> Tag<'a> { Some(self.get_unchecked(ind)) } - pub fn note(&self) -> &'a Note<'a> { - self.note + pub fn note(&self) -> &Note<'a> { + &self.note } pub fn as_ptr(&self) -> *mut bindings::ndb_tag { @@ -51,10 +51,10 @@ impl<'a> IntoIterator for Tag<'a> { } } -#[derive(Debug, Copy, Clone)] +#[derive(Debug, Clone)] pub struct Tags<'a> { ptr: *mut bindings::ndb_tags, - note: &'a Note<'a>, + note: Note<'a>, } impl<'a> IntoIterator for Tags<'a> { @@ -62,12 +62,12 @@ impl<'a> IntoIterator for Tags<'a> { type IntoIter = TagsIter<'a>; fn into_iter(self) -> Self::IntoIter { - TagsIter::new(self.note()) + TagsIter::new(self.note().clone()) } } impl<'a> Tags<'a> { - pub(crate) fn new(ptr: *mut bindings::ndb_tags, note: &'a Note<'a>) -> Self { + pub(crate) fn new(ptr: *mut bindings::ndb_tags, note: Note<'a>) -> Self { Tags { ptr, note } } @@ -76,11 +76,11 @@ impl<'a> Tags<'a> { } pub fn iter(&self) -> TagsIter<'a> { - TagsIter::new(self.note) + TagsIter::new(self.note.clone()) } - pub fn note(&self) -> &'a Note<'a> { - self.note + pub fn note(&self) -> &Note<'a> { + &self.note } pub fn as_ptr(&self) -> *mut bindings::ndb_tags { @@ -88,20 +88,23 @@ impl<'a> Tags<'a> { } } -#[derive(Debug, Copy, Clone)] +#[derive(Debug, Clone)] pub struct TagsIter<'a> { iter: bindings::ndb_iterator, - note: &'a Note<'a>, + note: Note<'a>, } impl<'a> TagsIter<'a> { - pub fn new(note: &'a Note<'a>) -> Self { + pub fn new(note: Note<'a>) -> Self { let iter = bindings::ndb_iterator { note: std::ptr::null_mut(), tag: std::ptr::null_mut(), index: 0, }; - let mut iter = TagsIter { note, iter }; + let mut iter = TagsIter { + note: note.clone(), + iter, + }; unsafe { bindings::ndb_tags_iterate_start(note.as_ptr(), &mut iter.iter); }; @@ -113,12 +116,12 @@ impl<'a> TagsIter<'a> { if tag_ptr.is_null() { None } else { - Some(Tag::new(tag_ptr, self.note())) + Some(Tag::new(tag_ptr, self.note().clone())) } } - pub fn note(&self) -> &'a Note<'a> { - self.note + pub fn note(&self) -> &Note<'a> { + &self.note } pub fn as_ptr(&self) -> *const bindings::ndb_iterator { @@ -130,7 +133,7 @@ impl<'a> TagsIter<'a> { } } -#[derive(Debug, Copy, Clone)] +#[derive(Debug, Clone)] pub struct TagIter<'a> { tag: Tag<'a>, index: u16,