Skip to content

Commit

Permalink
refactor: use LazyLock from the std
Browse files Browse the repository at this point in the history
  • Loading branch information
ronnychevalier committed Jul 28, 2024
1 parent 982c82b commit 7efa8e6
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 75 deletions.
96 changes: 48 additions & 48 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ description = "Source code orthotypography checker"
publish = false
license = "MIT OR Apache-2.0"
readme = "README.md"
rust-version = "1.79.0"
rust-version = "1.80.0"

[features]
default = ["lang-all"]
Expand Down
29 changes: 3 additions & 26 deletions src/lang.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::collections::HashMap;
use std::ffi::OsStr;
use std::ops::Deref;
use std::sync::{Arc, OnceLock};
use std::sync::{Arc, LazyLock};

use tree_sitter::{Node, Parser, Tree};

Expand All @@ -10,30 +9,7 @@ use crate::tree::PreorderTraversal;
#[cfg(feature = "lang-markdown")]
mod markdown;

struct Lazy<T> {
cell: OnceLock<T>,
init: fn() -> T,
}

impl<T> Lazy<T> {
pub const fn new(init: fn() -> T) -> Self {
Self {
cell: OnceLock::new(),
init,
}
}
}

impl<T> Deref for Lazy<T> {
type Target = T;

#[inline]
fn deref(&self) -> &'_ T {
self.cell.get_or_init(self.init)
}
}

static EXTENSION_LANGUAGE: Lazy<HashMap<&'static OsStr, Arc<Language>>> = Lazy::new(|| {
static EXTENSION_LANGUAGE: LazyLock<HashMap<&'static OsStr, Arc<Language>>> = LazyLock::new(|| {
let mut map = HashMap::new();

macro_rules! lang {
Expand Down Expand Up @@ -73,6 +49,7 @@ pub struct Language {

impl Language {
/// Find the language to parse based on a file extension
///
/// # Example
///
/// ```
Expand Down

0 comments on commit 7efa8e6

Please sign in to comment.