-
Notifications
You must be signed in to change notification settings - Fork 84
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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"); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -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 { | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could use the There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. Lines 15 to 17 in 56e7947
|
||||||||
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. | ||||||||
|
@@ -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; | ||||||||
|
@@ -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") | ||||||||
} | ||||||||
|
@@ -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, | ||||||||
|
@@ -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, | ||||||||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated docs.