Skip to content

Commit 7cd73e2

Browse files
committed
Allow using a few &Self-taking static methods as inherent &self methods
1 parent faa7692 commit 7cd73e2

File tree

1 file changed

+33
-33
lines changed

1 file changed

+33
-33
lines changed

Diff for: src/arc_str.rs

+33-33
Original file line numberDiff line numberDiff line change
@@ -494,23 +494,23 @@ impl ArcStr {
494494
/// ```
495495
/// # use arcstr::ArcStr;
496496
/// let foobar = ArcStr::from("foobar");
497-
/// assert_eq!(Some(1), ArcStr::strong_count(&foobar));
497+
/// assert_eq!(Some(1), foobar.strong_count());
498498
/// 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());
501501
/// ```
502502
///
503503
/// ### Static ArcStr
504504
/// ```
505505
/// # use arcstr::ArcStr;
506506
/// let baz = arcstr::literal!("baz");
507-
/// assert_eq!(None, ArcStr::strong_count(&baz));
507+
/// assert_eq!(None, baz.strong_count());
508508
/// // Similarly:
509-
/// assert_eq!(None, ArcStr::strong_count(&ArcStr::default()));
509+
/// assert_eq!(None, ArcStr::default().string_count());
510510
/// ```
511511
#[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)?;
514514
if cf.flag_part() {
515515
None
516516
} else {
@@ -556,13 +556,13 @@ impl ArcStr {
556556
/// # // doctests?). Instead, we test this in tests/arc_str.rs
557557
/// # use arcstr::ArcStr;
558558
/// 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());
561561
///
562562
/// let leaked: &'static str = s.leak();
563563
/// 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"));
566566
/// ```
567567
#[inline]
568568
pub fn leak(&self) -> &'static str {
@@ -617,33 +617,36 @@ impl ArcStr {
617617
Self::bytes_ptr(this) as *const str
618618
}
619619

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.
623623
///
624624
/// 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()`.
627630
///
628631
/// # Examples
629632
///
630633
/// ```
631634
/// # use arcstr::ArcStr;
632635
/// const STATIC: ArcStr = arcstr::literal!("Electricity!");
633-
/// assert!(ArcStr::is_static(&STATIC));
636+
/// assert!(STATIC.is_static());
634637
///
635638
/// let still_static = arcstr::literal!("Shocking!");
636-
/// assert!(ArcStr::is_static(&still_static));
639+
/// assert!(still_static.is_static());
637640
/// assert!(
638-
/// ArcStr::is_static(&still_static.clone()),
641+
/// still_static.is_static().clone()),
639642
/// "Cloned statics are still static"
640643
/// );
641644
///
642645
/// let nonstatic = ArcStr::from("Grounded...");
643-
/// assert!(!ArcStr::is_static(&nonstatic));
646+
/// assert!(!nonstatic.is_static());
644647
/// ```
645648
#[inline]
646-
pub fn is_static(this: &Self) -> bool {
649+
pub fn is_static(&self) -> bool {
647650
// We align this to 16 bytes and keep the `is_static` flags in the same
648651
// place. In theory this means that if `cfg(target_feature = "avx")`
649652
// (where aligned 16byte loads are atomic), the compiler *could*
@@ -661,8 +664,8 @@ impl ArcStr {
661664
// since Rust's semantics don't allow me to make that change
662665
// optimization on my own (that load isn't considered atomic, for
663666
// 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() }
666669
}
667670

668671
/// This is true for any `ArcStr` that has been static from the time when it
@@ -672,13 +675,10 @@ impl ArcStr {
672675
this.get_inner_len_flag().flag_part()
673676
}
674677

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.
682682
///
683683
/// # Examples
684684
///
@@ -698,10 +698,10 @@ impl ArcStr {
698698
/// assert_eq!(ArcStr::as_static(&nonstatic), None);
699699
/// ```
700700
#[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) {
703703
// 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) })
705705
} else {
706706
None
707707
}

0 commit comments

Comments
 (0)