Skip to content

Commit 5f0302c

Browse files
author
bors-servo
committed
Auto merge of #126 - servo:convert-traits, r=asajeffrey
Use conversion trait This is a breaking change: * `Atom::from_slice(…)` needs to be replaced with `Atom::from(…)` * `atom.as_slice().other_method()` with `atom.other_method()` * `atom.as_slice()` in another auto-deref context with `&atom` * `atom.as_slice()` in another context with either `&*atom` or `atom.as_ref()` r? @asajeffrey <!-- Reviewable:start --> [<img src="https://reviewable.io/review_button.png" height=40 alt="Review on Reviewable"/>](https://reviewable.io/reviews/servo/string-cache/126) <!-- Reviewable:end -->
2 parents ce7133c + d9f3133 commit 5f0302c

File tree

5 files changed

+93
-96
lines changed

5 files changed

+93
-96
lines changed

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: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use test::{Bencher, black_box};
3232

3333
// Just shorthand
3434
fn mk(x: &str) -> Atom {
35-
Atom::from_slice(x)
35+
Atom::from(x)
3636
}
3737

3838
macro_rules! check_type (($name:ident, $x:expr, $p:pat) => (
@@ -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(|| {
84-
black_box(Atom::from_slice(&x));
84+
black_box(Atom::from(&*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]
@@ -205,7 +205,7 @@ macro_rules! bench_rand ( ($name:ident, $len:expr) => (
205205
*n = (*n % 0x40) + 0x20;
206206
}
207207
let s = str::from_utf8(&buf[..]).unwrap();
208-
black_box(Atom::from_slice(s));
208+
black_box(Atom::from(s));
209209
});
210210
}
211211
));

src/atom/mod.rs

Lines changed: 64 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,11 @@ impl Atom {
140140
unsafe fn unpack(&self) -> UnpackedAtom {
141141
UnpackedAtom::from_packed(self.data)
142142
}
143+
}
143144

145+
impl<'a> From<&'a str> for Atom {
144146
#[inline]
145-
pub fn from_slice(string_to_add: &str) -> Atom {
147+
fn from(string_to_add: &str) -> Atom {
146148
let unpacked = match STATIC_ATOM_SET.get_index_or_hash(string_to_add) {
147149
Ok(id) => Static(id as u32),
148150
Err(hash) => {
@@ -161,23 +163,6 @@ impl Atom {
161163
log!(Event::Intern(data));
162164
Atom { data: data }
163165
}
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-
}
181166
}
182167

183168
impl Clone for Atom {
@@ -226,7 +211,19 @@ impl ops::Deref for Atom {
226211

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

@@ -248,7 +245,7 @@ impl fmt::Debug for Atom {
248245
}
249246
};
250247

251-
write!(f, "Atom('{}' type={})", self.as_slice(), ty_str)
248+
write!(f, "Atom('{}' type={})", &*self, ty_str)
252249
}
253250
}
254251

@@ -258,7 +255,7 @@ impl PartialOrd for Atom {
258255
if self.data == other.data {
259256
return Some(Equal);
260257
}
261-
self.as_slice().partial_cmp(other.as_slice())
258+
self.as_ref().partial_cmp(other.as_ref())
262259
}
263260
}
264261

@@ -268,7 +265,7 @@ impl Ord for Atom {
268265
if self.data == other.data {
269266
return Equal;
270267
}
271-
self.as_slice().cmp(other.as_slice())
268+
self.as_ref().cmp(other.as_ref())
272269
}
273270
}
274271

@@ -288,7 +285,7 @@ impl Serialize for Atom {
288285
impl Deserialize for Atom {
289286
fn deserialize<D>(deserializer: &mut D) -> Result<Atom,D::Error> where D: Deserializer {
290287
let string: String = try!(Deserialize::deserialize(deserializer));
291-
Ok(Atom::from_slice(&*string))
288+
Ok(Atom::from(&*string))
292289
}
293290
}
294291

@@ -304,27 +301,27 @@ mod tests {
304301

305302
#[test]
306303
fn test_as_slice() {
307-
let s0 = Atom::from_slice("");
308-
assert!(s0.as_slice() == "");
304+
let s0 = Atom::from("");
305+
assert!(s0.as_ref() == "");
309306

310-
let s1 = Atom::from_slice("class");
311-
assert!(s1.as_slice() == "class");
307+
let s1 = Atom::from("class");
308+
assert!(s1.as_ref() == "class");
312309

313-
let i0 = Atom::from_slice("blah");
314-
assert!(i0.as_slice() == "blah");
310+
let i0 = Atom::from("blah");
311+
assert!(i0.as_ref() == "blah");
315312

316-
let s0 = Atom::from_slice("BLAH");
317-
assert!(s0.as_slice() == "BLAH");
313+
let s0 = Atom::from("BLAH");
314+
assert!(s0.as_ref() == "BLAH");
318315

319-
let d0 = Atom::from_slice("zzzzzzzzzz");
320-
assert!(d0.as_slice() == "zzzzzzzzzz");
316+
let d0 = Atom::from("zzzzzzzzzz");
317+
assert!(d0.as_ref() == "zzzzzzzzzz");
321318

322-
let d1 = Atom::from_slice("ZZZZZZZZZZ");
323-
assert!(d1.as_slice() == "ZZZZZZZZZZ");
319+
let d1 = Atom::from("ZZZZZZZZZZ");
320+
assert!(d1.as_ref() == "ZZZZZZZZZZ");
324321
}
325322

326323
macro_rules! unpacks_to (($e:expr, $t:pat) => (
327-
match unsafe { Atom::from_slice($e).unpack() } {
324+
match unsafe { Atom::from($e).unpack() } {
328325
$t => (),
329326
_ => panic!("atom has wrong type"),
330327
}
@@ -348,17 +345,17 @@ mod tests {
348345

349346
#[test]
350347
fn test_equality() {
351-
let s0 = Atom::from_slice("fn");
352-
let s1 = Atom::from_slice("fn");
353-
let s2 = Atom::from_slice("loop");
348+
let s0 = Atom::from("fn");
349+
let s1 = Atom::from("fn");
350+
let s2 = Atom::from("loop");
354351

355-
let i0 = Atom::from_slice("blah");
356-
let i1 = Atom::from_slice("blah");
357-
let i2 = Atom::from_slice("blah2");
352+
let i0 = Atom::from("blah");
353+
let i1 = Atom::from("blah");
354+
let i2 = Atom::from("blah2");
358355

359-
let d0 = Atom::from_slice("zzzzzzzz");
360-
let d1 = Atom::from_slice("zzzzzzzz");
361-
let d2 = Atom::from_slice("zzzzzzzzz");
356+
let d0 = Atom::from("zzzzzzzz");
357+
let d1 = Atom::from("zzzzzzzz");
358+
let d2 = Atom::from("zzzzzzzzz");
362359

363360
assert!(s0 == s1);
364361
assert!(s0 != s2);
@@ -377,9 +374,9 @@ mod tests {
377374
#[test]
378375
fn ord() {
379376
fn check(x: &str, y: &str) {
380-
assert_eq!(x < y, Atom::from_slice(x) < Atom::from_slice(y));
381-
assert_eq!(x.cmp(y), Atom::from_slice(x).cmp(&Atom::from_slice(y)));
382-
assert_eq!(x.partial_cmp(y), Atom::from_slice(x).partial_cmp(&Atom::from_slice(y)));
377+
assert_eq!(x < y, Atom::from(x) < Atom::from(y));
378+
assert_eq!(x.cmp(y), Atom::from(x).cmp(&Atom::from(y)));
379+
assert_eq!(x.partial_cmp(y), Atom::from(x).partial_cmp(&Atom::from(y)));
383380
}
384381

385382
check("a", "body");
@@ -395,17 +392,17 @@ mod tests {
395392

396393
#[test]
397394
fn clone() {
398-
let s0 = Atom::from_slice("fn");
395+
let s0 = Atom::from("fn");
399396
let s1 = s0.clone();
400-
let s2 = Atom::from_slice("loop");
397+
let s2 = Atom::from("loop");
401398

402-
let i0 = Atom::from_slice("blah");
399+
let i0 = Atom::from("blah");
403400
let i1 = i0.clone();
404-
let i2 = Atom::from_slice("blah2");
401+
let i2 = Atom::from("blah2");
405402

406-
let d0 = Atom::from_slice("zzzzzzzz");
403+
let d0 = Atom::from("zzzzzzzz");
407404
let d1 = d0.clone();
408-
let d2 = Atom::from_slice("zzzzzzzzz");
405+
let d2 = Atom::from("zzzzzzzzz");
409406

410407
assert!(s0 == s1);
411408
assert!(s0 != s2);
@@ -434,12 +431,12 @@ mod tests {
434431
#[test]
435432
fn repr() {
436433
fn check(s: &str, data: u64) {
437-
assert_eq_fmt!("0x{:016X}", Atom::from_slice(s).data, data);
434+
assert_eq_fmt!("0x{:016X}", Atom::from(s).data, data);
438435
}
439436

440437
fn check_static(s: &str, x: Atom) {
441438
use string_cache_shared::STATIC_ATOM_SET;
442-
assert_eq_fmt!("0x{:016X}", x.data, Atom::from_slice(s).data);
439+
assert_eq_fmt!("0x{:016X}", x.data, Atom::from(s).data);
443440
assert_eq!(0x2, x.data & 0xFFFF_FFFF);
444441
// The index is unspecified by phf.
445442
assert!((x.data >> 32) <= STATIC_ATOM_SET.iter().len() as u64);
@@ -460,7 +457,7 @@ mod tests {
460457
check("xyzzy01", 0x3130_797A_7A79_7871);
461458

462459
// Dynamic atoms. This is a pointer so we can't verify every bit.
463-
assert_eq!(0x00, Atom::from_slice("a dynamic string").data & 0xf);
460+
assert_eq!(0x00, Atom::from("a dynamic string").data & 0xf);
464461
}
465462

466463
#[test]
@@ -475,34 +472,34 @@ mod tests {
475472
fn test_threads() {
476473
for _ in 0_u32..100 {
477474
thread::spawn(move || {
478-
let _ = Atom::from_slice("a dynamic string");
479-
let _ = Atom::from_slice("another string");
475+
let _ = Atom::from("a dynamic string");
476+
let _ = Atom::from("another string");
480477
});
481478
}
482479
}
483480

484481
#[test]
485482
fn atom_macro() {
486-
assert_eq!(atom!(body), Atom::from_slice("body"));
487-
assert_eq!(atom!("body"), Atom::from_slice("body"));
488-
assert_eq!(atom!("font-weight"), Atom::from_slice("font-weight"));
483+
assert_eq!(atom!(body), Atom::from("body"));
484+
assert_eq!(atom!("body"), Atom::from("body"));
485+
assert_eq!(atom!("font-weight"), Atom::from("font-weight"));
489486
}
490487

491488
#[test]
492489
fn match_atom() {
493-
assert_eq!(2, match Atom::from_slice("head") {
490+
assert_eq!(2, match Atom::from("head") {
494491
atom!(br) => 1,
495492
atom!(html) | atom!(head) => 2,
496493
_ => 3,
497494
});
498495

499-
assert_eq!(3, match Atom::from_slice("body") {
496+
assert_eq!(3, match Atom::from("body") {
500497
atom!(br) => 1,
501498
atom!(html) | atom!(head) => 2,
502499
_ => 3,
503500
});
504501

505-
assert_eq!(3, match Atom::from_slice("zzzzzz") {
502+
assert_eq!(3, match Atom::from("zzzzzz") {
506503
atom!(br) => 1,
507504
atom!(html) | atom!(head) => 2,
508505
_ => 3,
@@ -512,14 +509,14 @@ mod tests {
512509
#[test]
513510
fn ensure_deref() {
514511
// Ensure we can Deref to a &str
515-
let atom = Atom::from_slice("foobar");
512+
let atom = Atom::from("foobar");
516513
let _: &str = &atom;
517514
}
518515

519516
#[test]
520517
fn ensure_as_ref() {
521518
// Ensure we can as_ref to a &str
522-
let atom = Atom::from_slice("foobar");
519+
let atom = Atom::from("foobar");
523520
let _: &str = atom.as_ref();
524521
}
525522

0 commit comments

Comments
 (0)