Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Slab::new const on Rust 1.39+, Use #[track_caller] on Rust 1.46+ #119

Merged
merged 2 commits into from
Jul 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@ exclude = ["/.*"]
std = []
default = ["std"]

[build-dependencies]
autocfg = "1"

[dependencies]
serde = { version = "1.0.95", optional = true, default-features = false, features = ["alloc"] }

[dev-dependencies]
rustversion = "1"
serde = { version = "1", features = ["derive"] }
serde_test = "1"
24 changes: 24 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
fn main() {
let cfg = match autocfg::AutoCfg::new() {
Ok(cfg) => cfg,
Err(e) => {
// If we couldn't detect the compiler version and features, just
// print a warning. This isn't a fatal error: we can still build
// Slab, we just can't enable cfgs automatically.
println!(
"cargo:warning=slab: failed to detect compiler features: {}",
e
);
return;
}
};
// Note that this is `no_`*, not `has_*`. This allows treating as the latest
// stable rustc is used when the build script doesn't run. This is useful
// for non-cargo build systems that don't run the build script.
if !cfg.probe_rustc_version(1, 39) {
println!("cargo:rustc-cfg=slab_no_const_vec_new");
}
if !cfg.probe_rustc_version(1, 46) {
println!("cargo:rustc-cfg=slab_no_track_caller");
}
}
29 changes: 27 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,14 +219,35 @@ impl<T> Slab<T> {
/// The function does not allocate and the returned slab will have no
/// capacity until `insert` is called or capacity is explicitly reserved.
///
/// This is `const fn` on Rust 1.39+.
///
/// # Examples
///
/// ```
/// # use slab::*;
/// let slab: Slab<i32> = Slab::new();
/// ```
pub fn new() -> Slab<T> {
Slab::with_capacity(0)
#[cfg(not(slab_no_const_vec_new))]
pub const fn new() -> Self {
Comment on lines +230 to +231
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fact that const depends on the version should probably be documented.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated docs.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could use the const-fn crate here to avoid code deduplication.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is my crate, but it doesn't yet implement the behavior I want.

slab/build.rs

Lines 15 to 17 in 56e7947

// Note that this is `no_`*, not `has_*`. This allows treating as the latest
// stable rustc is used when the build script doesn't run. This is useful
// for non-cargo build systems that don't run the build script.

Self {
entries: Vec::new(),
next: 0,
len: 0,
}
}
/// Construct a new, empty `Slab`.
///
/// The function does not allocate and the returned slab will have no
/// capacity until `insert` is called or capacity is explicitly reserved.
///
/// This is `const fn` on Rust 1.39+.
#[cfg(slab_no_const_vec_new)]
pub fn new() -> Self {
Self {
entries: Vec::new(),
next: 0,
len: 0,
}
}

/// Construct a new, empty `Slab` with the specified capacity.
Expand Down Expand Up @@ -877,6 +898,7 @@ impl<T> Slab<T> {
/// slab.key_of(bad); // this will panic
/// unreachable!();
/// ```
#[cfg_attr(not(slab_no_track_caller), track_caller)]
pub fn key_of(&self, present_element: &T) -> usize {
let element_ptr = present_element as *const T as usize;
let base_ptr = self.entries.as_ptr() as usize;
Expand Down Expand Up @@ -1045,6 +1067,7 @@ impl<T> Slab<T> {
/// assert_eq!(slab.remove(hello), "hello");
/// assert!(!slab.contains(hello));
/// ```
#[cfg_attr(not(slab_no_track_caller), track_caller)]
pub fn remove(&mut self, key: usize) -> T {
self.try_remove(key).expect("invalid key")
}
Expand Down Expand Up @@ -1152,6 +1175,7 @@ impl<T> Slab<T> {
impl<T> ops::Index<usize> for Slab<T> {
type Output = T;

#[cfg_attr(not(slab_no_track_caller), track_caller)]
fn index(&self, key: usize) -> &T {
match self.entries.get(key) {
Some(&Entry::Occupied(ref v)) => v,
Expand All @@ -1161,6 +1185,7 @@ impl<T> ops::Index<usize> for Slab<T> {
}

impl<T> ops::IndexMut<usize> for Slab<T> {
#[cfg_attr(not(slab_no_track_caller), track_caller)]
fn index_mut(&mut self, key: usize) -> &mut T {
match self.entries.get_mut(key) {
Some(&mut Entry::Occupied(ref mut v)) => v,
Expand Down
6 changes: 6 additions & 0 deletions tests/slab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -696,3 +696,9 @@ fn try_remove() {
assert_eq!(slab.try_remove(key), None);
assert_eq!(slab.get(key), None);
}

#[rustversion::since(1.39)]
#[test]
fn const_new() {
static _SLAB: Slab<()> = Slab::new();
}