@@ -138,18 +138,36 @@ impl StringCache {
138138 }
139139}
140140
141+ /// A static `PhfStrSet`
142+ ///
143+ /// This trait is implemented by static sets of interned strings generated using
144+ /// `string_cache_codegen`, and `EmptyStaticAtomSet` for when strings will be added dynamically.
145+ ///
146+ /// It is used by the methods of [`Atom`] to check if a string is present in the static set.
147+ ///
148+ /// [`Atom`]: struct.Atom.html
141149pub trait StaticAtomSet {
150+ /// Get the location of the static string set in the binary.
142151 fn get ( ) -> & ' static PhfStrSet ;
152+ /// Get the index of the empty string, which is in every set and is used for `Atom::default`.
143153 fn empty_string_index ( ) -> u32 ;
144154}
145155
156+ /// A string set created using a [perfect hash function], specifically
157+ /// [Hash, Displace and Compress].
158+ ///
159+ /// See the CHD document for the meaning of the struct fields.
160+ ///
161+ /// [perfect hash function]: https://en.wikipedia.org/wiki/Perfect_hash_function
162+ /// [Hash, Displace and Compress]: http://cmph.sourceforge.net/papers/esa09.pdf
146163pub struct PhfStrSet {
147164 pub key : u64 ,
148165 pub disps : & ' static [ ( u32 , u32 ) ] ,
149166 pub atoms : & ' static [ & ' static str ] ,
150167 pub hashes : & ' static [ u32 ] ,
151168}
152169
170+ /// An empty static atom set for when only dynamic strings will be added
153171pub struct EmptyStaticAtomSet ;
154172
155173impl StaticAtomSet for EmptyStaticAtomSet {
@@ -174,6 +192,10 @@ impl StaticAtomSet for EmptyStaticAtomSet {
174192/// Use this if you don’t care about static atoms.
175193pub type DefaultAtom = Atom < EmptyStaticAtomSet > ;
176194
195+ /// Represents a string that has been interned.
196+ ///
197+ /// In reality this contains a complex packed datastructure and the methods to extract information
198+ /// from it, along with type information to tell the compiler which static set it corresponds to.
177199pub struct Atom < Static : StaticAtomSet > {
178200 /// This field is public so that the `atom!()` macros can use it.
179201 /// You should not otherwise access this field.
@@ -207,6 +229,7 @@ impl<Static: StaticAtomSet> Atom<Static> {
207229 UnpackedAtom :: from_packed ( self . unsafe_data )
208230 }
209231
232+ /// Get the hash of the string as it is stored in the set.
210233 pub fn get_hash ( & self ) -> u32 {
211234 match unsafe { self . unpack ( ) } {
212235 Static ( index) => {
@@ -441,11 +464,7 @@ impl<Static: StaticAtomSet> Atom<Static> {
441464 let mut buffer: [ u8 ; 64 ] = unsafe { mem:: uninitialized ( ) } ;
442465 if let Some ( buffer_prefix) = buffer. get_mut ( ..s. len ( ) ) {
443466 buffer_prefix. copy_from_slice ( s. as_bytes ( ) ) ;
444- // FIXME: use from std::str when stable https://github.com/rust-lang/rust/issues/41119
445- pub unsafe fn from_utf8_unchecked_mut ( v : & mut [ u8 ] ) -> & mut str {
446- mem:: transmute ( v)
447- }
448- let as_str = unsafe { from_utf8_unchecked_mut ( buffer_prefix) } ;
467+ let as_str = unsafe { :: std:: str:: from_utf8_unchecked_mut ( buffer_prefix) } ;
449468 f ( as_str) ;
450469 Atom :: from ( & * as_str)
451470 } else {
@@ -455,6 +474,9 @@ impl<Static: StaticAtomSet> Atom<Static> {
455474 }
456475 }
457476
477+ /// Like [`to_ascii_uppercase`].
478+ ///
479+ /// [`to_ascii_uppercase`]: https://doc.rust-lang.org/std/ascii/trait.AsciiExt.html#tymethod.to_ascii_uppercase
458480 pub fn to_ascii_uppercase ( & self ) -> Self {
459481 for ( i, b) in self . bytes ( ) . enumerate ( ) {
460482 if let b'a' ... b'z' = b {
@@ -464,6 +486,9 @@ impl<Static: StaticAtomSet> Atom<Static> {
464486 self . clone ( )
465487 }
466488
489+ /// Like [`to_ascii_lowercase`].
490+ ///
491+ /// [`to_ascii_lowercase`]: https://doc.rust-lang.org/std/ascii/trait.AsciiExt.html#tymethod.to_ascii_lowercase
467492 pub fn to_ascii_lowercase ( & self ) -> Self {
468493 for ( i, b) in self . bytes ( ) . enumerate ( ) {
469494 if let b'A' ... b'Z' = b {
@@ -473,10 +498,16 @@ impl<Static: StaticAtomSet> Atom<Static> {
473498 self . clone ( )
474499 }
475500
501+ /// Like [`eq_ignore_ascii_case`].
502+ ///
503+ /// [`eq_ignore_ascii_case`]: https://doc.rust-lang.org/std/ascii/trait.AsciiExt.html#tymethod.eq_ignore_ascii_case
476504 pub fn eq_ignore_ascii_case ( & self , other : & Self ) -> bool {
477505 ( self == other) || self . eq_str_ignore_ascii_case ( & * * other)
478506 }
479507
508+ /// Like [`eq_ignore_ascii_case`], but takes an unhashed string as `other`.
509+ ///
510+ /// [`eq_ignore_ascii_case`]: https://doc.rust-lang.org/std/ascii/trait.AsciiExt.html#tymethod.eq_ignore_ascii_case
480511 pub fn eq_str_ignore_ascii_case ( & self , other : & str ) -> bool {
481512 ( & * * self ) . eq_ignore_ascii_case ( other)
482513 }
@@ -525,6 +556,8 @@ fn inline_atom_slice_mut(x: &mut u64) -> &mut [u8] {
525556}
526557
527558impl UnpackedAtom {
559+ /// Pack a key, fitting it into a u64 with flags and data. See `string_cache_shared` for
560+ /// hints for the layout.
528561 #[ inline( always) ]
529562 unsafe fn pack ( self ) -> u64 {
530563 match self {
@@ -546,6 +579,7 @@ impl UnpackedAtom {
546579 }
547580 }
548581
582+ /// Unpack a key, extracting information from a single u64 into useable structs.
549583 #[ inline( always) ]
550584 unsafe fn from_packed ( data : u64 ) -> UnpackedAtom {
551585 debug_assert ! ( DYNAMIC_TAG == 0 ) ; // Dynamic is untagged
0 commit comments