Skip to content

Commit 58fb4a9

Browse files
committed
Update to filling drop
1 parent c4fa256 commit 58fb4a9

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

src/atom/mod.rs

+14-5
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ impl StringCache {
155155

156156
// NOTE: Deriving Eq here implies that a given string must always
157157
// be interned the same way.
158-
#[unsafe_no_drop_flag]
158+
#[unsafe_no_drop_flag] // See tests::atom_drop_is_idempotent
159159
#[derive(Eq, Hash, PartialEq)]
160160
pub struct Atom {
161161
/// This field is public so that the `atom!()` macro can use it.
@@ -236,10 +236,7 @@ impl Drop for Atom {
236236

237237
unsafe {
238238
match string_cache_shared::from_packed_dynamic(self.data) {
239-
// We use #[unsafe_no_drop_flag] so that Atom will be only 64
240-
// bits. That means we need to ignore a NULL pointer here,
241-
// which represents a value that was moved out.
242-
Some(entry) if !entry.is_null() => {
239+
Some(entry) => {
243240
let entry = entry as *mut StringCacheEntry;
244241
if (*entry).ref_count.fetch_sub(1, SeqCst) == 1 {
245242
drop_slow(self);
@@ -251,6 +248,7 @@ impl Drop for Atom {
251248
}
252249
}
253250

251+
254252
impl ops::Deref for Atom {
255253
type Target = str;
256254

@@ -548,4 +546,15 @@ mod tests {
548546
let atom = Atom::from_slice("foobar");
549547
let _: &str = atom.as_ref();
550548
}
549+
550+
/// Atom uses #[unsafe_no_drop_flag] to stay small, so drop() may be called more than once.
551+
/// In calls after the first one, the atom will be filled with a POST_DROP value.
552+
/// drop() must be a no-op in this case.
553+
#[test]
554+
fn atom_drop_is_idempotent() {
555+
unsafe {
556+
assert_eq!(::string_cache_shared::from_packed_dynamic(::std::mem::POST_DROP_U64), None);
557+
}
558+
}
559+
551560
}

src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#![feature(plugin, unsafe_no_drop_flag)]
1414
#![feature(slice_bytes, heap_api, hash_default)]
1515
#![deny(warnings)]
16-
#![cfg_attr(test, feature(test))]
16+
#![cfg_attr(test, feature(test, filling_drop))]
1717
#![cfg_attr(bench, feature(rand))]
1818
#![plugin(string_cache_plugin)]
1919

0 commit comments

Comments
 (0)