Skip to content

Commit 48473e3

Browse files
committed
database: add downcast methods for dyn NostrDatabase
Implement downcasting methods to the `NostrDatabase` trait, allowing easy conversion to concrete database types. Pull-Request: #845 Signed-off-by: Yuki Kishimoto <[email protected]>
1 parent a376709 commit 48473e3

File tree

6 files changed

+50
-0
lines changed

6 files changed

+50
-0
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ nostr-relay-builder = { version = "0.42", path = "./crates/nostr-relay-builder",
3333
nostr-relay-pool = { version = "0.42", path = "./crates/nostr-relay-pool", default-features = false }
3434
nostr-sdk = { version = "0.42", path = "./crates/nostr-sdk", default-features = false }
3535
reqwest = { version = "0.12", default-features = false }
36+
rustversion = "1.0"
3637
serde = { version = "1.0", default-features = false }
3738
serde_json = { version = "1.0", default-features = false }
3839
tempfile = "3.19"

crates/nostr-database/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ flatbuf = ["dep:flatbuffers"]
1919
flatbuffers = { version = "23.5", optional = true }
2020
lru.workspace = true
2121
nostr = { workspace = true, features = ["std"] }
22+
rustversion.workspace = true
2223
tokio = { workspace = true, features = ["sync"] }
2324

2425
[dev-dependencies]

crates/nostr-database/src/lib.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,43 @@ pub trait NostrDatabase: Any + Debug + Send + Sync {
193193
fn wipe(&self) -> BoxedFuture<Result<(), DatabaseError>>;
194194
}
195195

196+
#[rustversion::since(1.86)]
197+
impl dyn NostrDatabase {
198+
/// Upcast to [`Any`].
199+
#[inline]
200+
fn as_any(&self) -> &dyn Any {
201+
self
202+
}
203+
204+
/// Upcast to [`Any`].
205+
#[inline]
206+
fn as_any_arc(self: Arc<Self>) -> Arc<dyn Any + Send + Sync> {
207+
self
208+
}
209+
210+
/// Downcast [`NostrDatabase`] to a concrete type reference.
211+
///
212+
/// **Requires rustc >= 1.86!**
213+
#[inline]
214+
pub fn downcast_ref<T>(&self) -> Option<&T>
215+
where
216+
T: NostrDatabase,
217+
{
218+
self.as_any().downcast_ref()
219+
}
220+
221+
/// Downcast [`NostrDatabase`] to a concrete type.
222+
///
223+
/// **Requires rustc >= 1.86!**
224+
#[inline]
225+
pub fn downcast<T>(self: Arc<Self>) -> Option<Arc<T>>
226+
where
227+
T: NostrDatabase,
228+
{
229+
self.as_any_arc().downcast().ok()
230+
}
231+
}
232+
196233
#[cfg(test)]
197234
mod tests {
198235
use super::*;

crates/nostr-sdk/examples/lmdb.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,10 @@ async fn main() -> Result<()> {
5858
let events = client.database().query(filter).await?;
5959
println!("Events: {events:?}");
6060

61+
// Database downcasting to access to specific APIs
62+
if let Some(_lmdb) = client.database().downcast_ref::<NostrLMDB>() {
63+
// Access specific APIs here
64+
}
65+
6166
Ok(())
6267
}

crates/nostr-sdk/examples/nostrdb.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,10 @@ async fn main() -> Result<()> {
3535
let events = client.database().query(filter).await?;
3636
println!("Events: {events:?}");
3737

38+
// Database downcasting to access to specific APIs
39+
if let Some(ndb) = client.database().downcast_ref::<NdbDatabase>() {
40+
println!("Subscription counts: {}", ndb.subscription_count());
41+
}
42+
3843
Ok(())
3944
}

0 commit comments

Comments
 (0)