Skip to content

Commit

Permalink
Use std::sync::OnceLock instead of once_cell::OnceCell
Browse files Browse the repository at this point in the history
  • Loading branch information
reiyw committed Dec 1, 2024
1 parent dd83441 commit 1bd77bc
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 19 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

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

1 change: 0 additions & 1 deletion ssspam-bot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ maud = "0.23.0"
mp3-metadata = "0.3.4"
nom = "7.1.1"
notify = { version = "5.0.0", default-features = false, features = ["macos_kqueue"] }
once_cell = "1.14.0"
opentelemetry = "0.21.0"
opentelemetry_sdk = "0.21.2"
opentelemetry-otlp = "0.14.0"
Expand Down
30 changes: 13 additions & 17 deletions ssspam-bot/src/sound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{
ffi::OsStr,
fs,
path::{Path, PathBuf},
sync::{Arc, RwLock},
sync::{Arc, OnceLock, RwLock},
time::{Duration, SystemTime},
};

Expand All @@ -15,7 +15,6 @@ use notify::{
event::{CreateKind, ModifyKind, RenameMode},
Config, Event, EventKind, RecommendedWatcher, RecursiveMode, Watcher,
};
use once_cell::sync::OnceCell;
use rand::{rngs::StdRng, seq::IteratorRandom, SeedableRng};
use serenity::prelude::TypeMapKey;
use tokio::{runtime::Handle, sync::mpsc};
Expand Down Expand Up @@ -91,15 +90,15 @@ impl Metadata {
}
}

#[derive(Debug, Clone, Default, PartialEq, Eq)]
#[derive(Debug, Clone)]
pub struct SoundFile {
pub name: String,
pub path: PathBuf,

// Retrieving metadata requires file parsing and is time consuming. For most
// files, metadata is not needed immediately, so wrap in OnceCell to delay
// files, metadata is not needed immediately, so wrap in OnceLock to delay
// metadata retrieval.
metadata: OnceCell<Metadata>,
metadata: OnceLock<Metadata>,
}

impl SoundFile {
Expand All @@ -108,7 +107,7 @@ impl SoundFile {
Self {
name: path.as_ref().file_stem().unwrap().to_string_lossy().into(),
path: path.as_ref().into(),
metadata: OnceCell::new(),
metadata: OnceLock::new(),
}
}

Expand Down Expand Up @@ -199,7 +198,7 @@ where
}
}

#[derive(Debug, Clone, Default, PartialEq, Eq)]
#[derive(Debug, Clone)]
pub struct SoundStorage {
/// Lowercased name to [`Sound`].
sounds: BTreeMap<String, SoundFile>,
Expand Down Expand Up @@ -396,15 +395,12 @@ mod test {
storage.remove("d");
storage.remove("dadeisan");
assert_eq!(storage.len(), 1);
assert_eq!(storage.get("d"), None);
assert_eq!(storage.get("dadeisan"), None);
assert_eq!(
storage.get_random().unwrap(),
storage.get("sainou").unwrap()
);
assert!(storage.get("d").is_none());
assert!(storage.get("dadeisan").is_none());
assert_eq!(storage.get_random().unwrap().name, "sainou",);

storage.remove("sainou");
assert_eq!(storage.get_random(), None);
assert!(storage.get_random().is_none());
}

#[test]
Expand All @@ -414,9 +410,9 @@ mod test {
.join("tests/sound");
let storage = SoundStorage::load(sound_dir);
let sims = storage.calc_similarities("dadei");
assert_eq!(sims[0].1, storage.get("dadeisan").unwrap());
assert_eq!(sims[1].1, storage.get("d").unwrap());
assert_eq!(sims[2].1, storage.get("sainou").unwrap());
assert_eq!(sims[0].1.name, "dadeisan");
assert_eq!(sims[1].1.name, "d");
assert_eq!(sims[2].1.name, "sainou");
}

#[tokio::test]
Expand Down

0 comments on commit 1bd77bc

Please sign in to comment.