From b14c74b056ed79d470b2b652b8017a33363dcc06 Mon Sep 17 00:00:00 2001 From: Taiki Endo Date: Sun, 10 Jul 2022 02:09:58 +0900 Subject: [PATCH] Use #[track_caller] on Rust 1.46+ --- build.rs | 5 ++++- src/lib.rs | 4 ++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/build.rs b/build.rs index 28840e4..b60351a 100644 --- a/build.rs +++ b/build.rs @@ -4,7 +4,7 @@ fn main() { 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 - // Tokio, we just can't enable cfgs automatically. + // Slab, we just can't enable cfgs automatically. println!( "cargo:warning=slab: failed to detect compiler features: {}", e @@ -18,4 +18,7 @@ fn main() { 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"); + } } diff --git a/src/lib.rs b/src/lib.rs index e369f46..577f7b9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -898,6 +898,7 @@ impl Slab { /// 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; @@ -1066,6 +1067,7 @@ impl Slab { /// 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") } @@ -1173,6 +1175,7 @@ impl Slab { impl ops::Index for Slab { 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, @@ -1182,6 +1185,7 @@ impl ops::Index for Slab { } impl ops::IndexMut for Slab { + #[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,