Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ nostr-relay-builder = { version = "0.42", path = "./crates/nostr-relay-builder",
nostr-relay-pool = { version = "0.42", path = "./crates/nostr-relay-pool", default-features = false }
nostr-sdk = { version = "0.42", path = "./crates/nostr-sdk", default-features = false }
reqwest = { version = "0.12", default-features = false }
rustversion = "1.0"
serde = { version = "1.0", default-features = false }
serde_json = { version = "1.0", default-features = false }
tempfile = "3.19"
Expand Down
1 change: 1 addition & 0 deletions crates/nostr-database/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ flatbuf = ["dep:flatbuffers"]
flatbuffers = { version = "23.5", optional = true }
lru.workspace = true
nostr = { workspace = true, features = ["std"] }
rustversion.workspace = true
tokio = { workspace = true, features = ["sync"] }

[dev-dependencies]
Expand Down
37 changes: 37 additions & 0 deletions crates/nostr-database/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,43 @@ pub trait NostrDatabase: Any + Debug + Send + Sync {
fn wipe(&self) -> BoxedFuture<Result<(), DatabaseError>>;
}

#[rustversion::since(1.86)]
impl dyn NostrDatabase {
/// Upcast to [`Any`].
#[inline]
fn as_any(&self) -> &dyn Any {
self
}

/// Upcast to [`Any`].
#[inline]
fn as_any_arc(self: Arc<Self>) -> Arc<dyn Any + Send + Sync> {
self
}

/// Downcast [`NostrDatabase`] to a concrete type reference.
///
/// **Requires rustc >= 1.86!**
#[inline]
pub fn downcast_ref<T>(&self) -> Option<&T>
where
T: NostrDatabase,
{
self.as_any().downcast_ref()
}

/// Downcast [`NostrDatabase`] to a concrete type.
///
/// **Requires rustc >= 1.86!**
#[inline]
pub fn downcast<T>(self: Arc<Self>) -> Option<Arc<T>>
where
T: NostrDatabase,
{
self.as_any_arc().downcast().ok()
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
5 changes: 5 additions & 0 deletions crates/nostr-sdk/examples/lmdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,10 @@ async fn main() -> Result<()> {
let events = client.database().query(filter).await?;
println!("Events: {events:?}");

// Database downcasting to access to specific APIs
if let Some(_lmdb) = client.database().downcast_ref::<NostrLMDB>() {
// Access specific APIs here
}

Ok(())
}
5 changes: 5 additions & 0 deletions crates/nostr-sdk/examples/nostrdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,10 @@ async fn main() -> Result<()> {
let events = client.database().query(filter).await?;
println!("Events: {events:?}");

// Database downcasting to access to specific APIs
if let Some(ndb) = client.database().downcast_ref::<NdbDatabase>() {
println!("Subscription counts: {}", ndb.subscription_count());
}

Ok(())
}
Loading