Skip to content

Commit f4c5583

Browse files
committed
nostr: use AllocMap and AllocSet in Filter
1 parent 2439a3d commit f4c5583

File tree

2 files changed

+24
-20
lines changed

2 files changed

+24
-20
lines changed

crates/nostr-database/src/index.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ struct FilterIndex {
116116
kinds: HashSet<Kind>,
117117
since: Option<Timestamp>,
118118
until: Option<Timestamp>,
119-
generic_tags: HashMap<Alphabet, BTreeSet<GenericTagValue>>,
119+
generic_tags: HashMap<Alphabet, HashSet<GenericTagValue>>,
120120
}
121121

122122
impl FilterIndex {
@@ -157,6 +157,7 @@ impl FilterIndex {
157157
if self.generic_tags.is_empty() {
158158
return true;
159159
}
160+
160161
if event.tags.is_empty() {
161162
return false;
162163
}
@@ -188,16 +189,16 @@ impl FilterIndex {
188189
impl From<Filter> for FilterIndex {
189190
fn from(value: Filter) -> Self {
190191
Self {
191-
ids: value.ids.into_iter().collect(),
192+
ids: value.ids,
192193
authors: value
193194
.authors
194195
.into_iter()
195196
.map(PublicKeyPrefix::from)
196197
.collect(),
197-
kinds: value.kinds.into_iter().collect(),
198+
kinds: value.kinds,
198199
since: value.since,
199200
until: value.until,
200-
generic_tags: value.generic_tags.into_iter().collect(),
201+
generic_tags: value.generic_tags,
201202
}
202203
}
203204
}

crates/nostr/src/message/subscription.rs

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@
55

66
//! Subscription filters
77
8-
use alloc::collections::{BTreeMap, BTreeSet};
8+
#[cfg(not(feature = "std"))]
9+
use alloc::collections::{BTreeMap as AllocMap, BTreeSet as AllocSet};
910
use alloc::string::{String, ToString};
1011
use core::fmt;
1112
use core::str::FromStr;
13+
#[cfg(feature = "std")]
14+
use std::collections::{HashMap as AllocMap, HashSet as AllocSet};
1215

1316
use bitcoin::hashes::sha256::Hash as Sha256Hash;
1417
use bitcoin::hashes::Hash;
@@ -316,17 +319,17 @@ impl IntoGenericTagValue for &str {
316319
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
317320
pub struct Filter {
318321
/// List of [`EventId`]
319-
#[serde(skip_serializing_if = "BTreeSet::is_empty")]
322+
#[serde(skip_serializing_if = "AllocSet::is_empty")]
320323
#[serde(default)]
321-
pub ids: BTreeSet<EventId>,
324+
pub ids: AllocSet<EventId>,
322325
/// List of [`XOnlyPublicKey`]
323-
#[serde(skip_serializing_if = "BTreeSet::is_empty")]
326+
#[serde(skip_serializing_if = "AllocSet::is_empty")]
324327
#[serde(default)]
325-
pub authors: BTreeSet<XOnlyPublicKey>,
328+
pub authors: AllocSet<XOnlyPublicKey>,
326329
/// List of a kind numbers
327-
#[serde(skip_serializing_if = "BTreeSet::is_empty")]
330+
#[serde(skip_serializing_if = "AllocSet::is_empty")]
328331
#[serde(default)]
329-
pub kinds: BTreeSet<Kind>,
332+
pub kinds: AllocSet<Kind>,
330333
/// It's a string describing a query in a human-readable form, i.e. "best nostr apps"
331334
///
332335
/// <https://github.com/nostr-protocol/nips/blob/master/50.md>
@@ -352,7 +355,7 @@ pub struct Filter {
352355
deserialize_with = "deserialize_generic_tags"
353356
)]
354357
#[serde(default)]
355-
pub generic_tags: BTreeMap<Alphabet, BTreeSet<GenericTagValue>>,
358+
pub generic_tags: AllocMap<Alphabet, AllocSet<GenericTagValue>>,
356359
}
357360

358361
impl Filter {
@@ -644,7 +647,7 @@ impl Filter {
644647
I: IntoIterator<Item = T>,
645648
T: IntoGenericTagValue,
646649
{
647-
let values: BTreeSet<GenericTagValue> = values
650+
let values: AllocSet<GenericTagValue> = values
648651
.into_iter()
649652
.map(|v| v.into_generic_tag_value())
650653
.collect();
@@ -663,7 +666,7 @@ impl Filter {
663666
I: IntoIterator<Item = T>,
664667
T: IntoGenericTagValue,
665668
{
666-
let values: BTreeSet<GenericTagValue> = values
669+
let values: AllocSet<GenericTagValue> = values
667670
.into_iter()
668671
.map(|v| v.into_generic_tag_value())
669672
.collect();
@@ -684,7 +687,7 @@ impl JsonUtil for Filter {
684687
}
685688

686689
fn serialize_generic_tags<S>(
687-
generic_tags: &BTreeMap<Alphabet, BTreeSet<GenericTagValue>>,
690+
generic_tags: &AllocMap<Alphabet, AllocSet<GenericTagValue>>,
688691
serializer: S,
689692
) -> Result<S::Ok, S::Error>
690693
where
@@ -699,14 +702,14 @@ where
699702

700703
fn deserialize_generic_tags<'de, D>(
701704
deserializer: D,
702-
) -> Result<BTreeMap<Alphabet, BTreeSet<GenericTagValue>>, D::Error>
705+
) -> Result<AllocMap<Alphabet, AllocSet<GenericTagValue>>, D::Error>
703706
where
704707
D: Deserializer<'de>,
705708
{
706709
struct GenericTagsVisitor;
707710

708711
impl<'de> Visitor<'de> for GenericTagsVisitor {
709-
type Value = BTreeMap<Alphabet, BTreeSet<GenericTagValue>>;
712+
type Value = AllocMap<Alphabet, AllocSet<GenericTagValue>>;
710713

711714
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
712715
formatter.write_str("map in which the keys are \"#X\" for some character X")
@@ -716,13 +719,13 @@ where
716719
where
717720
M: MapAccess<'de>,
718721
{
719-
let mut generic_tags = BTreeMap::new();
722+
let mut generic_tags = AllocMap::new();
720723
while let Some(key) = map.next_key::<String>()? {
721724
let mut chars = key.chars();
722725
if let (Some('#'), Some(ch), None) = (chars.next(), chars.next(), chars.next()) {
723726
let tag: Alphabet = Alphabet::from_str(ch.to_string().as_str())
724727
.map_err(serde::de::Error::custom)?;
725-
let mut values: BTreeSet<GenericTagValue> = map.next_value()?;
728+
let mut values: AllocSet<GenericTagValue> = map.next_value()?;
726729

727730
match tag {
728731
Alphabet::P => values.retain(|v| matches!(v, GenericTagValue::Pubkey(_))),
@@ -800,7 +803,7 @@ mod test {
800803
}
801804

802805
#[test]
803-
#[cfg(feature = "std")]
806+
#[cfg(not(feature = "std"))]
804807
fn test_filter_serialization() {
805808
let filter = Filter::new()
806809
.identifier("identifier")

0 commit comments

Comments
 (0)