@@ -494,23 +494,23 @@ impl ArcStr {
494
494
/// ```
495
495
/// # use arcstr::ArcStr;
496
496
/// let foobar = ArcStr::from("foobar");
497
- /// assert_eq!(Some(1), ArcStr:: strong_count(&foobar ));
497
+ /// assert_eq!(Some(1), foobar. strong_count());
498
498
/// let also_foobar = ArcStr::clone(&foobar);
499
- /// assert_eq!(Some(2), ArcStr:: strong_count(&foobar ));
500
- /// assert_eq!(Some(2), ArcStr:: strong_count(&also_foobar ));
499
+ /// assert_eq!(Some(2), foobar. strong_count());
500
+ /// assert_eq!(Some(2), also_foobar. strong_count());
501
501
/// ```
502
502
///
503
503
/// ### Static ArcStr
504
504
/// ```
505
505
/// # use arcstr::ArcStr;
506
506
/// let baz = arcstr::literal!("baz");
507
- /// assert_eq!(None, ArcStr:: strong_count(&baz ));
507
+ /// assert_eq!(None, baz. strong_count());
508
508
/// // Similarly:
509
- /// assert_eq!(None, ArcStr::strong_count(&ArcStr:: default()));
509
+ /// assert_eq!(None, ArcStr::default().string_count( ));
510
510
/// ```
511
511
#[ inline]
512
- pub fn strong_count ( this : & Self ) -> Option < usize > {
513
- let cf = Self :: load_count_flag ( this , Ordering :: Acquire ) ?;
512
+ pub fn strong_count ( & self ) -> Option < usize > {
513
+ let cf = Self :: load_count_flag ( self , Ordering :: Acquire ) ?;
514
514
if cf. flag_part ( ) {
515
515
None
516
516
} else {
@@ -556,13 +556,13 @@ impl ArcStr {
556
556
/// # // doctests?). Instead, we test this in tests/arc_str.rs
557
557
/// # use arcstr::ArcStr;
558
558
/// let s = ArcStr::from("foobar");
559
- /// assert!(!ArcStr:: is_static(&s ));
560
- /// assert!(ArcStr:: as_static(&s ).is_none());
559
+ /// assert!(!s. is_static());
560
+ /// assert!(s. as_static().is_none());
561
561
///
562
562
/// let leaked: &'static str = s.leak();
563
563
/// assert_eq!(leaked, s);
564
- /// assert!(ArcStr:: is_static(&s ));
565
- /// assert_eq!(ArcStr:: as_static(&s ), Some("foobar"));
564
+ /// assert!(s. is_static());
565
+ /// assert_eq!(s. as_static(), Some("foobar"));
566
566
/// ```
567
567
#[ inline]
568
568
pub fn leak ( & self ) -> & ' static str {
@@ -617,33 +617,36 @@ impl ArcStr {
617
617
Self :: bytes_ptr ( this) as * const str
618
618
}
619
619
620
- /// Returns true if `this ` is a "static" ArcStr. For example, if it was
621
- /// created from a call to [`arcstr::literal!`][crate::literal]),
622
- /// returned by `ArcStr::new`, etc.
620
+ /// Returns true if `self ` is a "static" ArcStr. For example, if it was
621
+ /// created from a call to [`arcstr::literal!`][crate::literal]), returned
622
+ /// by [ `ArcStr::new`], previously leaked with [`ArcStr::leak`] , etc.
623
623
///
624
624
/// Static `ArcStr`s can be converted to `&'static str` for free using
625
- /// [`ArcStr::as_static`], without leaking memory — they're static constants
626
- /// in the program (somewhere).
625
+ /// [`ArcStr::as_static`], without leaking memory — they're either static
626
+ /// constants in the program, or they're strings which have already been
627
+ /// deliberately leaked earlier (via [`ArcStr::leak`]).
628
+ ///
629
+ /// This function is morally equivalent to `self.as_static().is_some()`.
627
630
///
628
631
/// # Examples
629
632
///
630
633
/// ```
631
634
/// # use arcstr::ArcStr;
632
635
/// const STATIC: ArcStr = arcstr::literal!("Electricity!");
633
- /// assert!(ArcStr:: is_static(&STATIC ));
636
+ /// assert!(STATIC. is_static());
634
637
///
635
638
/// let still_static = arcstr::literal!("Shocking!");
636
- /// assert!(ArcStr:: is_static(&still_static ));
639
+ /// assert!(still_static. is_static());
637
640
/// assert!(
638
- /// ArcStr:: is_static(&still_static .clone()),
641
+ /// still_static. is_static() .clone()),
639
642
/// "Cloned statics are still static"
640
643
/// );
641
644
///
642
645
/// let nonstatic = ArcStr::from("Grounded...");
643
- /// assert!(!ArcStr:: is_static(&nonstatic ));
646
+ /// assert!(!nonstatic. is_static());
644
647
/// ```
645
648
#[ inline]
646
- pub fn is_static ( this : & Self ) -> bool {
649
+ pub fn is_static ( & self ) -> bool {
647
650
// We align this to 16 bytes and keep the `is_static` flags in the same
648
651
// place. In theory this means that if `cfg(target_feature = "avx")`
649
652
// (where aligned 16byte loads are atomic), the compiler *could*
@@ -661,8 +664,8 @@ impl ArcStr {
661
664
// since Rust's semantics don't allow me to make that change
662
665
// optimization on my own (that load isn't considered atomic, for
663
666
// example).
664
- this . get_inner_len_flag ( ) . flag_part ( )
665
- || unsafe { Self :: load_count_flag_raw ( this , Ordering :: Relaxed ) . flag_part ( ) }
667
+ self . get_inner_len_flag ( ) . flag_part ( )
668
+ || unsafe { Self :: load_count_flag_raw ( self , Ordering :: Relaxed ) . flag_part ( ) }
666
669
}
667
670
668
671
/// This is true for any `ArcStr` that has been static from the time when it
@@ -672,13 +675,10 @@ impl ArcStr {
672
675
this. get_inner_len_flag ( ) . flag_part ( )
673
676
}
674
677
675
- /// Returns true if `this` is a "static"/`"literal"` ArcStr. For example, if
676
- /// it was created from a call to [`literal!`][crate::literal]), returned by
677
- /// `ArcStr::new`, etc.
678
- ///
679
- /// Static `ArcStr`s can be converted to `&'static str` for free using
680
- /// [`ArcStr::as_static`], without leaking memory — they're static constants
681
- /// in the program (somewhere).
678
+ /// Returns `Some` of the string data as a `&'static str` if `self` is a
679
+ /// "static" ArcStr. For example, if it was created from a call to
680
+ /// [`literal!`][crate::literal]), returned by [`ArcStr::new`], previously
681
+ /// leaked with [`ArcStr::leak`], etc.
682
682
///
683
683
/// # Examples
684
684
///
@@ -698,10 +698,10 @@ impl ArcStr {
698
698
/// assert_eq!(ArcStr::as_static(&nonstatic), None);
699
699
/// ```
700
700
#[ inline]
701
- pub fn as_static ( this : & Self ) -> Option < & ' static str > {
702
- if Self :: is_static ( this ) {
701
+ pub fn as_static ( & self ) -> Option < & ' static str > {
702
+ if Self :: is_static ( self ) {
703
703
// We know static strings live forever, so they can have a static lifetime.
704
- Some ( unsafe { & * ( this . as_str ( ) as * const str ) } )
704
+ Some ( unsafe { & * ( self . as_str ( ) as * const str ) } )
705
705
} else {
706
706
None
707
707
}
0 commit comments