Skip to content

Commit de5ea4d

Browse files
committed
Remove Atom::as_slice in favor of Deref and AsRef
1 parent ce7133c commit de5ea4d

4 files changed

Lines changed: 34 additions & 39 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22

33
name = "string_cache"
4-
version = "0.1.17"
4+
version = "0.2.0"
55
authors = [ "The Servo Project Developers" ]
66
description = "A string interning library for Rust, developed as part of the Servo project."
77
license = "MIT / Apache-2.0"

examples/summarize-events/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ fn main() {
8484

8585
// FIXME: We really shouldn't be allowed to do this. It's a memory-safety
8686
// hazard; the field is only public for the atom!() macro.
87-
_ => Atom { data: ev.id }.as_slice().to_string(),
87+
_ => Atom { data: ev.id }.to_string(),
8888
};
8989

9090
match summary.entry(string) {

src/atom/bench.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,20 +79,20 @@ macro_rules! bench_one (
7979
(intern $x:expr, $_y:expr) => (
8080
#[bench]
8181
fn intern(b: &mut Bencher) {
82-
let x = $x.as_slice().to_string();
82+
let x = $x.to_string();
8383
b.iter(|| {
8484
black_box(Atom::from_slice(&x));
8585
});
8686
}
8787
);
8888

89-
(as_slice $x:expr, $_y:expr) => (
89+
(as_ref $x:expr, $_y:expr) => (
9090
#[bench]
91-
fn as_slice_x_1000(b: &mut Bencher) {
91+
fn as_ref_x_1000(b: &mut Bencher) {
9292
let x = $x;
9393
b.iter(|| {
9494
for _ in 0..1000 {
95-
black_box(x.as_slice());
95+
black_box(x.as_ref());
9696
}
9797
});
9898
}
@@ -156,22 +156,22 @@ bench_all!([eq ne lt clone_string] for medium_string = "xyzzy01", "xyzzy02");
156156
bench_all!([eq ne lt clone_string]
157157
for longer_string = super::longer_dynamic_a, super::longer_dynamic_b);
158158

159-
bench_all!([eq ne intern as_slice clone is_static lt]
159+
bench_all!([eq ne intern as_ref clone is_static lt]
160160
for static_atom = atom!(a), atom!(b));
161161

162-
bench_all!([intern as_slice clone is_inline]
162+
bench_all!([intern as_ref clone is_inline]
163163
for short_inline_atom = mk("e"), mk("f"));
164164

165-
bench_all!([eq ne intern as_slice clone is_inline lt]
165+
bench_all!([eq ne intern as_ref clone is_inline lt]
166166
for medium_inline_atom = mk("xyzzy01"), mk("xyzzy02"));
167167

168-
bench_all!([intern as_slice clone is_dynamic]
168+
bench_all!([intern as_ref clone is_dynamic]
169169
for min_dynamic_atom = mk("xyzzy001"), mk("xyzzy002"));
170170

171-
bench_all!([eq ne intern as_slice clone is_dynamic lt]
171+
bench_all!([eq ne intern as_ref clone is_dynamic lt]
172172
for longer_dynamic_atom = mk(super::longer_dynamic_a), mk(super::longer_dynamic_b));
173173

174-
bench_all!([intern as_slice clone is_static]
174+
bench_all!([intern as_ref clone is_static]
175175
for static_at_runtime = mk("a"), mk("b"));
176176

177177
bench_all!([ne lt x_static y_inline]

src/atom/mod.rs

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -161,23 +161,6 @@ impl Atom {
161161
log!(Event::Intern(data));
162162
Atom { data: data }
163163
}
164-
165-
#[inline]
166-
pub fn as_slice<'t>(&'t self) -> &'t str {
167-
unsafe {
168-
match self.unpack() {
169-
Inline(..) => {
170-
let buf = string_cache_shared::inline_orig_bytes(&self.data);
171-
str::from_utf8(buf).unwrap()
172-
},
173-
Static(idx) => STATIC_ATOM_SET.index(idx).expect("bad static atom"),
174-
Dynamic(entry) => {
175-
let entry = entry as *mut StringCacheEntry;
176-
&(*entry).string
177-
}
178-
}
179-
}
180-
}
181164
}
182165

183166
impl Clone for Atom {
@@ -226,7 +209,19 @@ impl ops::Deref for Atom {
226209

227210
#[inline]
228211
fn deref(&self) -> &str {
229-
self.as_slice()
212+
unsafe {
213+
match self.unpack() {
214+
Inline(..) => {
215+
let buf = string_cache_shared::inline_orig_bytes(&self.data);
216+
str::from_utf8(buf).unwrap()
217+
},
218+
Static(idx) => STATIC_ATOM_SET.index(idx).expect("bad static atom"),
219+
Dynamic(entry) => {
220+
let entry = entry as *mut StringCacheEntry;
221+
&(*entry).string
222+
}
223+
}
224+
}
230225
}
231226
}
232227

@@ -248,7 +243,7 @@ impl fmt::Debug for Atom {
248243
}
249244
};
250245

251-
write!(f, "Atom('{}' type={})", self.as_slice(), ty_str)
246+
write!(f, "Atom('{}' type={})", &*self, ty_str)
252247
}
253248
}
254249

@@ -258,7 +253,7 @@ impl PartialOrd for Atom {
258253
if self.data == other.data {
259254
return Some(Equal);
260255
}
261-
self.as_slice().partial_cmp(other.as_slice())
256+
self.as_ref().partial_cmp(other.as_ref())
262257
}
263258
}
264259

@@ -268,7 +263,7 @@ impl Ord for Atom {
268263
if self.data == other.data {
269264
return Equal;
270265
}
271-
self.as_slice().cmp(other.as_slice())
266+
self.as_ref().cmp(other.as_ref())
272267
}
273268
}
274269

@@ -305,22 +300,22 @@ mod tests {
305300
#[test]
306301
fn test_as_slice() {
307302
let s0 = Atom::from_slice("");
308-
assert!(s0.as_slice() == "");
303+
assert!(s0.as_ref() == "");
309304

310305
let s1 = Atom::from_slice("class");
311-
assert!(s1.as_slice() == "class");
306+
assert!(s1.as_ref() == "class");
312307

313308
let i0 = Atom::from_slice("blah");
314-
assert!(i0.as_slice() == "blah");
309+
assert!(i0.as_ref() == "blah");
315310

316311
let s0 = Atom::from_slice("BLAH");
317-
assert!(s0.as_slice() == "BLAH");
312+
assert!(s0.as_ref() == "BLAH");
318313

319314
let d0 = Atom::from_slice("zzzzzzzzzz");
320-
assert!(d0.as_slice() == "zzzzzzzzzz");
315+
assert!(d0.as_ref() == "zzzzzzzzzz");
321316

322317
let d1 = Atom::from_slice("ZZZZZZZZZZ");
323-
assert!(d1.as_slice() == "ZZZZZZZZZZ");
318+
assert!(d1.as_ref() == "ZZZZZZZZZZ");
324319
}
325320

326321
macro_rules! unpacks_to (($e:expr, $t:pat) => (

0 commit comments

Comments
 (0)