Skip to content

Commit b7ed526

Browse files
GKFXemilio
authored andcommitted
Remove lazy_static and lazycell dependencies
1 parent 79e970d commit b7ed526

File tree

9 files changed

+40
-55
lines changed

9 files changed

+40
-55
lines changed

.github/workflows/bindgen.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,20 @@ jobs:
5050
with:
5151
# MSRV below is documented in Cargo.toml and README.md, please update those if you
5252
# change this.
53-
toolchain: 1.60.0
53+
toolchain: 1.70.0
5454

5555
- name: Test lib with msrv
56-
run: cargo +1.60.0 test --package bindgen
56+
run: cargo +1.70.0 test --package bindgen
5757

5858
- name: Install msrv for cli
5959
uses: dtolnay/rust-toolchain@master
6060
with:
6161
# MSRV below is documented in Cargo.toml and README.md, please update those if you
6262
# change this.
63-
toolchain: 1.64.0
63+
toolchain: 1.70.0
6464

6565
- name: Test cli with msrv
66-
run: cargo +1.64.0 build --package bindgen-cli
66+
run: cargo +1.70.0 build --package bindgen-cli
6767

6868
minimal:
6969
runs-on: ubuntu-latest

Cargo.lock

-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ extern "C" {
3939

4040
## MSRV
4141

42-
The `bindgen` minimum supported Rust version is **1.60.0**.
42+
The `bindgen` minimum supported Rust version is **1.70.0**.
4343

44-
The `bindgen-cli` minimum supported Rust version is **1.64.0**.
44+
The `bindgen-cli` minimum supported Rust version is **1.70.0**.
4545

4646
No MSRV bump policy has been established yet, so MSRV may increase in any release.
4747

bindgen-cli/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ documentation = "https://docs.rs/bindgen"
1313
homepage = "https://rust-lang.github.io/rust-bindgen/"
1414
version = "0.69.4"
1515
edition = "2018"
16-
rust-version = "1.64.0"
16+
rust-version = "1.70.0"
1717

1818
[[bin]]
1919
path = "main.rs"

bindgen-tests/tests/quickchecking/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name = "quickchecking"
33
description = "Bindgen property tests with quickcheck. Generate random valid C code and pass it to the csmith/predicate.py script"
44
version = "0.0.0"
55
publish = false
6-
rust-version = "1.64"
6+
rust-version = "1.70"
77
edition = "2018"
88

99
[lib]

bindgen/Cargo.toml

+1-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ version = "0.69.4"
1818
edition = "2018"
1919
build = "build.rs"
2020
# If you change this, also update README.md and msrv in .github/workflows/bindgen.yml
21-
rust-version = "1.60.0"
21+
rust-version = "1.70.0"
2222

2323
[lib]
2424
name = "bindgen"
@@ -30,8 +30,6 @@ bitflags = "2.2.1"
3030
cexpr = "0.6"
3131
clang-sys = { version = "1", features = ["clang_6_0"] }
3232
itertools = { version = ">=0.10,<0.13", default-features = false }
33-
lazy_static = "1"
34-
lazycell = "1"
3533
log = { version = "0.4", optional = true }
3634
prettyplease = { version = "0.2.7", optional = true, features = ["verbatim"] }
3735
proc-macro2 = { version = "1", default-features = false }

bindgen/clang.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use std::hash::Hash;
1515
use std::hash::Hasher;
1616
use std::os::raw::{c_char, c_int, c_longlong, c_uint, c_ulong, c_ulonglong};
1717
use std::{mem, ptr, slice};
18+
use std::sync::OnceLock;
1819

1920
/// Type representing a clang attribute.
2021
///
@@ -1528,13 +1529,13 @@ impl Type {
15281529
pub(crate) fn is_associated_type(&self) -> bool {
15291530
// This is terrible :(
15301531
fn hacky_parse_associated_type<S: AsRef<str>>(spelling: S) -> bool {
1531-
lazy_static! {
1532-
static ref ASSOC_TYPE_RE: regex::Regex = regex::Regex::new(
1533-
r"typename type\-parameter\-\d+\-\d+::.+"
1534-
)
1535-
.unwrap();
1536-
}
1537-
ASSOC_TYPE_RE.is_match(spelling.as_ref())
1532+
static ASSOC_TYPE_RE: OnceLock<regex::Regex> = OnceLock::new();
1533+
ASSOC_TYPE_RE
1534+
.get_or_init(|| {
1535+
regex::Regex::new(r"typename type\-parameter\-\d+\-\d+::.+")
1536+
.unwrap()
1537+
})
1538+
.is_match(spelling.as_ref())
15381539
}
15391540

15401541
self.kind() == CXType_Unexposed &&

bindgen/ir/item.rs

+15-17
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,12 @@ use super::ty::{Type, TypeKind};
2020
use crate::clang;
2121
use crate::parse::{ClangSubItemParser, ParseError, ParseResult};
2222

23-
use lazycell::LazyCell;
24-
25-
use std::cell::Cell;
23+
use std::cell::{Cell, OnceCell};
2624
use std::collections::BTreeSet;
2725
use std::fmt::Write;
2826
use std::io;
2927
use std::iter;
28+
use std::sync::OnceLock;
3029

3130
/// A trait to get the canonical name from an item.
3231
///
@@ -380,7 +379,7 @@ pub(crate) struct Item {
380379
///
381380
/// Note that only structs, unions, and enums get a local type ID. In any
382381
/// case this is an implementation detail.
383-
local_id: LazyCell<usize>,
382+
local_id: OnceCell<usize>,
384383

385384
/// The next local ID to use for a child or template instantiation.
386385
next_child_local_id: Cell<usize>,
@@ -389,11 +388,11 @@ pub(crate) struct Item {
389388
///
390389
/// This is a fairly used operation during codegen so this makes bindgen
391390
/// considerably faster in those cases.
392-
canonical_name: LazyCell<String>,
391+
canonical_name: OnceCell<String>,
393392

394393
/// The path to use for allowlisting and other name-based checks, as
395394
/// returned by `path_for_allowlisting`, lazily constructed.
396-
path_for_allowlisting: LazyCell<Vec<String>>,
395+
path_for_allowlisting: OnceCell<Vec<String>>,
397396

398397
/// A doc comment over the item, if any.
399398
comment: Option<String>,
@@ -431,10 +430,10 @@ impl Item {
431430
debug_assert!(id != parent_id || kind.is_module());
432431
Item {
433432
id,
434-
local_id: LazyCell::new(),
433+
local_id: OnceCell::new(),
435434
next_child_local_id: Cell::new(1),
436-
canonical_name: LazyCell::new(),
437-
path_for_allowlisting: LazyCell::new(),
435+
canonical_name: OnceCell::new(),
436+
path_for_allowlisting: OnceCell::new(),
438437
parent_id,
439438
comment,
440439
annotations: annotations.unwrap_or_default(),
@@ -535,7 +534,7 @@ impl Item {
535534
/// below this item's lexical scope, meaning that this can be useful for
536535
/// generating relatively stable identifiers within a scope.
537536
pub(crate) fn local_id(&self, ctx: &BindgenContext) -> usize {
538-
*self.local_id.borrow_with(|| {
537+
*self.local_id.get_or_init(|| {
539538
let parent = ctx.resolve_item(self.parent_id);
540539
parent.next_child_local_id()
541540
})
@@ -1037,7 +1036,7 @@ impl Item {
10371036
ctx: &BindgenContext,
10381037
) -> &Vec<String> {
10391038
self.path_for_allowlisting
1040-
.borrow_with(|| self.compute_path(ctx, UserMangled::No))
1039+
.get_or_init(|| self.compute_path(ctx, UserMangled::No))
10411040
}
10421041

10431042
fn compute_path(
@@ -1807,10 +1806,9 @@ impl Item {
18071806
refd: &clang::Cursor,
18081807
spelling: &str,
18091808
) -> bool {
1810-
lazy_static! {
1811-
static ref ANON_TYPE_PARAM_RE: regex::Regex =
1812-
regex::Regex::new(r"^type\-parameter\-\d+\-\d+$").unwrap();
1813-
}
1809+
static ANON_TYPE_PARAM_RE: OnceLock<regex::Regex> = OnceLock::new();
1810+
let anon_type_param_re = ANON_TYPE_PARAM_RE.get_or_init(||
1811+
regex::Regex::new(r"^type\-parameter\-\d+\-\d+$").unwrap());
18141812

18151813
if refd.kind() != clang_sys::CXCursor_TemplateTypeParameter {
18161814
return false;
@@ -1819,7 +1817,7 @@ impl Item {
18191817
let refd_spelling = refd.spelling();
18201818
refd_spelling == spelling ||
18211819
// Allow for anonymous template parameters.
1822-
(refd_spelling.is_empty() && ANON_TYPE_PARAM_RE.is_match(spelling.as_ref()))
1820+
(refd_spelling.is_empty() && anon_type_param_re.is_match(spelling.as_ref()))
18231821
}
18241822

18251823
let definition = if is_template_with_spelling(&location, &ty_spelling) {
@@ -1909,7 +1907,7 @@ impl ItemCanonicalName for Item {
19091907
"You're not supposed to call this yet"
19101908
);
19111909
self.canonical_name
1912-
.borrow_with(|| {
1910+
.get_or_init(|| {
19131911
let in_namespace = ctx.options().enable_cxx_namespaces ||
19141912
ctx.options().disable_name_namespacing;
19151913

bindgen/lib.rs

+8-12
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
#[macro_use]
2020
extern crate bitflags;
2121
#[macro_use]
22-
extern crate lazy_static;
23-
#[macro_use]
2422
extern crate quote;
2523

2624
#[cfg(feature = "logging")]
@@ -77,6 +75,7 @@ use std::path::{Path, PathBuf};
7775
use std::process::{Command, Stdio};
7876
use std::rc::Rc;
7977
use std::str::FromStr;
78+
use std::sync::{Arc, OnceLock};
8079

8180
// Some convenient typedefs for a fast hash map and hash set.
8281
type HashMap<K, V> = rustc_hash::FxHashMap<K, V>;
@@ -617,17 +616,14 @@ fn ensure_libclang_is_loaded() {
617616
// doesn't get dropped prematurely, nor is loaded multiple times
618617
// across different threads.
619618

620-
lazy_static! {
621-
static ref LIBCLANG: std::sync::Arc<clang_sys::SharedLibrary> = {
622-
clang_sys::load().expect("Unable to find libclang");
623-
clang_sys::get_library().expect(
624-
"We just loaded libclang and it had better still be \
625-
here!",
626-
)
627-
};
628-
}
619+
static LIBCLANG: OnceLock<Arc<clang_sys::SharedLibrary>> = OnceLock::new();
620+
let libclang = LIBCLANG.get_or_init(|| {
621+
clang_sys::load().expect("Unable to find libclang");
622+
clang_sys::get_library()
623+
.expect("We just loaded libclang and it had better still be here!")
624+
});
629625

630-
clang_sys::set_library(Some(LIBCLANG.clone()));
626+
clang_sys::set_library(Some(libclang.clone()));
631627
}
632628

633629
#[cfg(not(feature = "runtime"))]

0 commit comments

Comments
 (0)