Ver Fonte

Use iterator type instead of taking a generic

It was a mistake, my skills got better
Cesar Rodas há 10 meses atrás
pai
commit
f8033bf03b

+ 2 - 5
crates/relayer/src/relayer.rs

@@ -18,10 +18,7 @@ type SubId = u128;
 
 type Subscriptions = HashMap<SubId, (SubscriptionId, Sender<Response>)>;
 
-pub struct Relayer<'a, I, T: Storage<'a, I>>
-where
-    I: Iterator<Item = Result<Event, nostr_rs_storage::Error>>,
-{
+pub struct Relayer<T: Storage> {
     /// Storage engine, if provided the services are going to persisted in disk,
     /// otherwise all the messages are going to be ephemeral, making this
     /// relayer just a dumb proxy (that can be useful for privacy) but it won't
@@ -46,7 +43,7 @@ where
     _phantom: std::marker::PhantomData<&'a I>,
 }
 
-impl<'a, I, T: Storage<'a, I>> Relayer<'a, I, T>
+impl<T: Storage> Relayer<T>
 where
     I: Iterator<Item = Result<Event, nostr_rs_storage::Error>>,
 {

+ 24 - 36
crates/storage/src/lib.rs

@@ -25,10 +25,9 @@ mod test {
         io::{BufRead, BufReader},
     };
 
-    fn setup_db<'a, T, I>(db: &'a T)
+    fn setup_db<T>(db: &T)
     where
-        T: Storage<'a, I>,
-        I: Iterator<Item = Result<Event, Error>>,
+        T: Storage,
     {
         let file = File::open("./tests/events.json").expect("file");
         let events = BufReader::new(file)
@@ -41,10 +40,9 @@ mod test {
         }
     }
 
-    pub fn store_and_get<'a, T, I>(db: &'a T)
+    pub fn store_and_get<T>(db: &T)
     where
-        T: Storage<'a, I>,
-        I: Iterator<Item = Result<Event, Error>>,
+        T: Storage,
     {
         let json = "{\"content\":\"{\\\"lud06\\\":\\\"lnbc1p3a4wxvpp5x0pa6gr55fq5s9d3dxs0vz77mqxgdw63hhtgtlfz5zvm65847vnqdqqcqpjsp5402c8rtqxd4j97rnvuejuwl4sg473g6wg08d67fvn7qc4gtpkfks9q7sqqqqqqqqqqqqqqqqqqqsqqqqqysgqmqz9gxqyjw5qrzjqwryaup9lh50kkranzgcdnn2fgvx390wgj5jd07rwr3vxeje0glclleasn65surjcsqqqqlgqqqqqeqqjqyxj968tem9ps6ttm9ukv6ag4yc6qmgj2svrccfgp4n83fpktr3dsx6fq7grfzlqt982aaemahg9q29vzl9f627kh4j8h8xc2z2mtpdqqjlekah\\\",\\\"website\\\":\\\"\\\",\\\"nip05\\\":\\\"cesar@cesar.com.py\\\",\\\"picture\\\":\\\"https://pbs.twimg.com/profile_images/1175432935337537536/_Peu9vuJ_400x400.jpg\\\",\\\"display_name\\\":\\\"C\\\",\\\"about\\\":\\\"Rust and PHP\\\",\\\"name\\\":\\\"c\\\"}\",\"created_at\":1678476588,\"id\":\"3800c787a23288641c0b96cbcc87c26cbd3ea7bee53b7748422fdb100fb7b9f0\",\"kind\":0,\"pubkey\":\"b2815682cfc83fcd2c3add05785cf4573dd388457069974cc6d8cca06b3c3b78\",\"sig\":\"c8a12ce96833e4cd67bce0e9e50f831262ef0f0c0cff5e56c38a0c90867ed1a6621e9692948ef5e85a7ca3726c3f0f43fa7e1992536bc457317123bca8784f5f\",\"tags\":[]}";
 
@@ -56,10 +54,9 @@ mod test {
         assert_eq!(event1, Some(event));
     }
 
-    pub fn records_are_sorted_by_date_desc<'a, T, I>(db: &'a T)
+    pub fn records_are_sorted_by_date_desc<T>(db: &T)
     where
-        T: Storage<'a, I>,
-        I: Iterator<Item = Result<Event, Error>>,
+        T: Storage,
     {
         setup_db(db);
 
@@ -85,10 +82,9 @@ mod test {
         assert_eq!(dates, sorted_dates);
     }
 
-    pub fn filter_by_references<'a, T, I>(db: &'a T)
+    pub fn filter_by_references<T>(db: &T)
     where
-        T: Storage<'a, I>,
-        I: Iterator<Item = Result<Event, Error>>,
+        T: Storage,
     {
         setup_db(db);
 
@@ -112,10 +108,9 @@ mod test {
         assert_eq!(related_events.len(), 1);
     }
 
-    pub fn filter_by_references_zero_match<'a, T, I>(db: &'a T)
+    pub fn filter_by_references_zero_match<T>(db: &T)
     where
-        T: Storage<'a, I>,
-        I: Iterator<Item = Result<Event, Error>>,
+        T: Storage,
     {
         setup_db(db);
 
@@ -139,10 +134,9 @@ mod test {
         assert_eq!(related_events.len(), 0);
     }
 
-    pub fn filter_by_references_and_kind<'a, T, I>(db: &'a T)
+    pub fn filter_by_references_and_kind<T>(db: &T)
     where
-        T: Storage<'a, I>,
-        I: Iterator<Item = Result<Event, Error>>,
+        T: Storage,
     {
         setup_db(db);
 
@@ -162,10 +156,9 @@ mod test {
         assert_eq!(related_events.len(), 3);
     }
 
-    pub fn get_event_and_related_events<'a, T, I>(db: &'a T)
+    pub fn get_event_and_related_events<T>(db: &T)
     where
-        T: Storage<'a, I>,
-        I: Iterator<Item = Result<Event, Error>>,
+        T: Storage,
     {
         setup_db(db);
 
@@ -202,10 +195,9 @@ mod test {
         assert_eq!(Kind::Unknown(42), kinds[1]);
     }
 
-    pub fn filter_by_authors<'a, T, I>(db: &'a T)
+    pub fn filter_by_authors<T>(db: &T)
     where
-        T: Storage<'a, I>,
-        I: Iterator<Item = Result<Event, Error>>,
+        T: Storage,
     {
         setup_db(db);
         let query = Filter {
@@ -227,10 +219,9 @@ mod test {
         assert_eq!(records.len(), 27);
     }
 
-    pub fn filter_by_author<'a, T, I>(db: &'a T)
+    pub fn filter_by_author<T>(db: &T)
     where
-        T: Storage<'a, I>,
-        I: Iterator<Item = Result<Event, Error>>,
+        T: Storage,
     {
         setup_db(db);
         let query = Filter {
@@ -249,10 +240,9 @@ mod test {
         assert_eq!(records.len(), 3);
     }
 
-    pub fn filter_by_author_and_kinds<'a, T, I>(db: &'a T)
+    pub fn filter_by_author_and_kinds<T>(db: &T)
     where
-        T: Storage<'a, I>,
-        I: Iterator<Item = Result<Event, Error>>,
+        T: Storage,
     {
         setup_db(db);
         let query = Filter {
@@ -272,10 +262,9 @@ mod test {
         assert_eq!(records.len(), 2);
     }
 
-    pub fn filter_kind<'a, T, I>(db: &'a T)
+    pub fn filter_kind<T>(db: &T)
     where
-        T: Storage<'a, I>,
-        I: Iterator<Item = Result<Event, Error>>,
+        T: Storage,
     {
         setup_db(db);
         let query = Filter {
@@ -294,10 +283,9 @@ mod test {
             .for_each(|x| assert_eq!(x, Kind::ShortTextNote));
     }
 
-    pub fn get_local_events<'a, T, I>(db: &'a T)
+    pub fn get_local_events<T>(db: &T)
     where
-        T: Storage<'a, I>,
-        I: Iterator<Item = Result<Event, Error>>,
+        T: Storage,
     {
         setup_db(db);
 

+ 4 - 3
crates/storage/src/rocksdb/mod.rs

@@ -85,8 +85,9 @@ impl RocksDb {
     }
 }
 
-impl<'a> Storage<'a, WrapperIterator<'a>> for RocksDb {
-    fn get_local_events(&'a self, limit: Option<usize>) -> Result<WrapperIterator<'a>, Error> {
+impl Storage for RocksDb {
+    type Iterator<'a> = WrapperIterator<'a>;
+    fn get_local_events<'a>(&'a self, limit: Option<usize>) -> Result<WrapperIterator<'a>, Error> {
         let cf_handle = self.reference_to_cf_handle(ReferenceType::LocalEvents)?;
         Ok(WrapperIterator {
             db: self,
@@ -199,7 +200,7 @@ impl<'a> Storage<'a, WrapperIterator<'a>> for RocksDb {
             .transpose()?)
     }
 
-    fn get_by_filter(&'a self, mut query: Filter) -> Result<WrapperIterator<'a>, Error> {
+    fn get_by_filter<'a>(&'a self, mut query: Filter) -> Result<WrapperIterator<'a>, Error> {
         let limit = if query.limit == 0 {
             None
         } else {

+ 8 - 6
crates/storage/src/storage.rs

@@ -2,10 +2,12 @@ use crate::Error;
 use nostr_rs_types::types::{Event, Filter};
 
 /// Trait to store/query events
-pub trait Storage<'a, I>
-where
-    I: Iterator<Item = Result<Event, Error>>,
-{
+pub trait Storage {
+    /// Result iterators
+    type Iterator<'a>: Iterator<Item = Result<Event, Error>>
+    where
+        Self: 'a;
+
     /// Stores a new event into the database. This action will also creates all
     /// the needed indexes to find this event later by reference, author, kind or tag.
     fn store(&self, event: &Event) -> Result<bool, Error>;
@@ -22,10 +24,10 @@ where
     /// The first step is to use one available index to get a list of event-IDs,
     /// then call `load_and_filter_events` that will load the events from the
     /// `Events` namespace and will filter them by the given parameters.
-    fn get_by_filter(&'a self, query: Filter) -> Result<I, Error>;
+    fn get_by_filter<'a>(&'a self, query: Filter) -> Result<Self::Iterator<'a>, Error>;
 
     /// Return a vector of all local events
-    fn get_local_events(&'a self, limit: Option<usize>) -> Result<I, Error>;
+    fn get_local_events<'a>(&'a self, limit: Option<usize>) -> Result<Self::Iterator<'a>, Error>;
 
     /// Stores an event, similar to store(Event), but keeps track of this event in a
     /// local index. This is useful to keep track of the events that are created by