Skip to content

Commit 6e83f71

Browse files
authored
Remove dashmap (cf #4408) (#4521)
* chore: remove `dashmap` (cf. #4408) * chore: remove conditional code for wasm32 from stores * fix merge issue * merge issues * clippy * remove dashmap in example
2 parents 26121c2 + 16a3237 commit 6e83f71

File tree

12 files changed

+119
-161
lines changed

12 files changed

+119
-161
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ actix-web = { default-features = false, version = "4.12" }
9090
tracing = { default-features = false, version = "0.1" }
9191
slotmap = { default-features = false, version = "1.1" }
9292
futures = { default-features = false, version = "0.3" }
93-
dashmap = { default-features = false, version = "6.1" }
9493
pin-project-lite = { default-features = false, version = "0.2" }
9594
send_wrapper = { default-features = false, version = "0.6" }
9695
tokio-test = { default-features = false, version = "0.4" }

examples/server_fns_axum/Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ web-sys = { version = "0.3.70", features = ["FileList", "File"] }
3737
strum = { version = "0.27.1", features = ["strum_macros", "derive"] }
3838
notify = { version = "8.0", optional = true }
3939
pin-project-lite = "0.2.14"
40-
dashmap = { version = "6.0", optional = true }
4140
async-broadcast = { version = "0.7.1", optional = true }
4241
bytecheck = "0.8.0"
4342
rkyv = { version = "0.8.8" }
@@ -52,7 +51,6 @@ ssr = [
5251
"leptos/ssr",
5352
"dep:leptos_axum",
5453
"dep:notify",
55-
"dep:dashmap",
5654
"dep:async-broadcast",
5755
]
5856

examples/server_fns_axum/src/app.rs

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -422,40 +422,43 @@ pub fn FileUploadWithProgress() -> impl IntoView {
422422
#[cfg(feature = "ssr")]
423423
mod progress {
424424
use async_broadcast::{broadcast, Receiver, Sender};
425-
use dashmap::DashMap;
426425
use futures::Stream;
427-
use std::sync::LazyLock;
426+
use std::{
427+
collections::HashMap,
428+
sync::{LazyLock, RwLock},
429+
};
428430

429431
struct File {
430432
total: usize,
431433
tx: Sender<usize>,
432434
rx: Receiver<usize>,
433435
}
434436

435-
static FILES: LazyLock<DashMap<String, File>> =
436-
LazyLock::new(DashMap::new);
437+
static FILES: LazyLock<RwLock<HashMap<String, File>>> =
438+
LazyLock::new(|| RwLock::new(HashMap::new()));
437439

438440
pub async fn add_chunk(filename: &str, len: usize) {
439441
println!("[{filename}]\tadding {len}");
440-
let mut entry =
441-
FILES.entry(filename.to_string()).or_insert_with(|| {
442-
println!("[{filename}]\tinserting channel");
443-
// NOTE: this channel capacity is set arbitrarily for this demo code.
444-
// it allows for up to exactly 1048 chunks to be sent, which sets an upper cap
445-
// on upload size (the precise details vary by client)
446-
// in a real system, you will want to create some more reasonable ways of
447-
// sending and sharing notifications
448-
//
449-
// see https://github.com/leptos-rs/leptos/issues/4397 for related discussion
450-
let (tx, rx) = broadcast(1048);
451-
File { total: 0, tx, rx }
452-
});
453-
entry.total += len;
454-
let new_total = entry.total;
455-
456-
// we're about to do an async broadcast, so we don't want to hold a lock across it
457-
let tx = entry.tx.clone();
458-
drop(entry);
442+
let (tx, new_total) = {
443+
let mut lock = FILES.write().unwrap();
444+
let entry =
445+
lock.entry(filename.to_string()).or_insert_with(|| {
446+
println!("[{filename}]\tinserting channel");
447+
// NOTE: this channel capacity is set arbitrarily for this demo code.
448+
// it allows for up to exactly 1048 chunks to be sent, which sets an upper cap
449+
// on upload size (the precise details vary by client)
450+
// in a real system, you will want to create some more reasonable ways of
451+
// sending and sharing notifications
452+
//
453+
// see https://github.com/leptos-rs/leptos/issues/4397 for related discussion
454+
let (tx, rx) = broadcast(1048);
455+
File { total: 0, tx, rx }
456+
});
457+
entry.total += len;
458+
let new_total = entry.total;
459+
460+
(entry.tx.clone(), new_total)
461+
};
459462

460463
// now we send the message and don't have to worry about it
461464
tx.broadcast(new_total)
@@ -464,12 +467,12 @@ pub fn FileUploadWithProgress() -> impl IntoView {
464467
}
465468

466469
pub fn for_file(filename: &str) -> impl Stream<Item = usize> {
467-
let entry =
468-
FILES.entry(filename.to_string()).or_insert_with(|| {
469-
println!("[{filename}]\tinserting channel");
470-
let (tx, rx) = broadcast(128);
471-
File { total: 0, tx, rx }
472-
});
470+
let mut lock = FILES.write().unwrap();
471+
let entry = lock.entry(filename.to_string()).or_insert_with(|| {
472+
println!("[{filename}]\tinserting channel");
473+
let (tx, rx) = broadcast(128);
474+
File { total: 0, tx, rx }
475+
});
473476
entry.rx.clone()
474477
}
475478
}

integrations/actix/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ serde_json = { workspace = true, default-features = true }
2626
tracing = { optional = true, workspace = true, default-features = true }
2727
tokio = { features = ["rt", "fs"], workspace = true, default-features = true }
2828
send_wrapper = { workspace = true, default-features = true }
29-
dashmap = { workspace = true, default-features = true }
30-
or_poisoned = { workspace = true, default-features = true }
29+
or_poisoned = { workspace = true, default-features = true }
3130

3231
[package.metadata.docs.rs]
3332
all-features = true

integrations/actix/src/lib.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ use actix_web::{
1616
web::{Data, Payload, ServiceConfig},
1717
*,
1818
};
19-
use dashmap::DashMap;
2019
use futures::{stream::once, Stream, StreamExt};
2120
use http::StatusCode;
2221
use hydration_context::SsrSharedContext;
@@ -45,7 +44,7 @@ use server_fn::{
4544
request::actix::ActixRequest,
4645
};
4746
use std::{
48-
collections::HashSet,
47+
collections::{HashMap, HashSet},
4948
fmt::{Debug, Display},
5049
future::Future,
5150
ops::{Deref, DerefMut},
@@ -1223,8 +1222,9 @@ impl StaticRouteGenerator {
12231222
}
12241223
}
12251224

1226-
static STATIC_HEADERS: LazyLock<DashMap<String, ResponseOptions>> =
1227-
LazyLock::new(DashMap::new);
1225+
static STATIC_HEADERS: LazyLock<
1226+
std::sync::RwLock<HashMap<String, ResponseOptions>>,
1227+
> = LazyLock::new(Default::default);
12281228

12291229
fn was_404(owner: &Owner) -> bool {
12301230
let resp = owner.with(|| expect_context::<ResponseOptions>());
@@ -1256,7 +1256,10 @@ async fn write_static_route(
12561256
html: &str,
12571257
) -> Result<(), std::io::Error> {
12581258
if let Some(options) = response_options {
1259-
STATIC_HEADERS.insert(path.to_string(), options);
1259+
STATIC_HEADERS
1260+
.write()
1261+
.or_poisoned()
1262+
.insert(path.to_string(), options);
12601263
}
12611264

12621265
let path = static_path(options, path);
@@ -1323,8 +1326,11 @@ where
13231326
.await;
13241327
(owner.with(use_context::<ResponseOptions>), html)
13251328
} else {
1326-
let headers =
1327-
STATIC_HEADERS.get(orig_path).map(|v| v.clone());
1329+
let headers = STATIC_HEADERS
1330+
.read()
1331+
.or_poisoned()
1332+
.get(orig_path)
1333+
.cloned();
13281334
(headers, None)
13291335
};
13301336

integrations/axum/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ hydration_context = { workspace = true }
1414
axum = { default-features = false, features = [
1515
"matched-path",
1616
], workspace = true }
17-
dashmap = { workspace = true, default-features = true }
1817
futures = { workspace = true, default-features = true }
1918
leptos = { workspace = true, features = ["nonce", "ssr"] }
2019
server_fn = { workspace = true, features = ["axum-no-default"] }

integrations/axum/src/lib.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ use axum::{
4747
response::IntoResponse,
4848
routing::{delete, get, patch, post, put},
4949
};
50-
#[cfg(feature = "default")]
51-
use dashmap::DashMap;
5250
use futures::{stream::once, Future, Stream, StreamExt};
5351
use hydration_context::SsrSharedContext;
5452
use leptos::{
@@ -72,9 +70,9 @@ use leptos_router::{
7270
use or_poisoned::OrPoisoned;
7371
use server_fn::{error::ServerFnErrorErr, redirect::REDIRECT_HEADER};
7472
#[cfg(feature = "default")]
75-
use std::path::Path;
76-
#[cfg(feature = "default")]
7773
use std::sync::LazyLock;
74+
#[cfg(feature = "default")]
75+
use std::{collections::HashMap, path::Path};
7876
use std::{
7977
collections::HashSet,
8078
fmt::Debug,
@@ -1533,8 +1531,9 @@ impl StaticRouteGenerator {
15331531
}
15341532

15351533
#[cfg(feature = "default")]
1536-
static STATIC_HEADERS: LazyLock<DashMap<String, ResponseOptions>> =
1537-
LazyLock::new(DashMap::new);
1534+
static STATIC_HEADERS: LazyLock<
1535+
std::sync::RwLock<HashMap<String, ResponseOptions>>,
1536+
> = LazyLock::new(Default::default);
15381537

15391538
#[cfg(feature = "default")]
15401539
fn was_404(owner: &Owner) -> bool {
@@ -1569,7 +1568,10 @@ async fn write_static_route(
15691568
html: &str,
15701569
) -> Result<(), std::io::Error> {
15711570
if let Some(options) = response_options {
1572-
STATIC_HEADERS.insert(path.to_string(), options);
1571+
STATIC_HEADERS
1572+
.write()
1573+
.or_poisoned()
1574+
.insert(path.to_string(), options);
15731575
}
15741576

15751577
let path = static_path(options, path);
@@ -1646,7 +1648,8 @@ where
16461648
.await;
16471649
(owner.with(use_context::<ResponseOptions>), html)
16481650
} else {
1649-
let headers = STATIC_HEADERS.get(orig_path).map(|v| v.clone());
1651+
let headers =
1652+
STATIC_HEADERS.read().or_poisoned().get(orig_path).cloned();
16501653
(headers, None)
16511654
};
16521655

reactive_stores/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ paste = { workspace = true, default-features = true }
1717
reactive_graph = { workspace = true }
1818
rustc-hash = { workspace = true, default-features = true }
1919
reactive_stores_macro = { workspace = true }
20-
dashmap = { workspace = true, default-features = true }
2120
send_wrapper = { workspace = true, default-features = true }
2221

2322
[dev-dependencies]

0 commit comments

Comments
 (0)