Skip to content

Commit

Permalink
Format and add document some
Browse files Browse the repository at this point in the history
  • Loading branch information
novacrazy committed Dec 19, 2022
1 parent ec33a30 commit d3d5cb8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
14 changes: 12 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -515,10 +515,16 @@ mod serde {
}
}

/// Convert value to [`SmolStr`] using [`fmt::Display`], potentially without allocating.
///
/// Almost identical to [`ToString`], but converts to `SmolStr` instead.
pub trait ToSmolStr {
fn to_smolstr(&self) -> SmolStr;
}

/// Formats arguments to a [`SmolStr`], potentially without allocating.
///
/// See [`alloc::format!`] or [`format_args!`] for syntax documentation.
#[macro_export]
macro_rules! format_smolstr {
($($tt:tt)*) => {{
Expand Down Expand Up @@ -562,7 +568,11 @@ impl fmt::Write for Writer {
self.heap.reserve(self.len);

// copy existing inline bytes over to the heap
unsafe { self.heap.as_mut_vec().extend_from_slice(&self.inline[..old_len]); }
unsafe {
self.heap
.as_mut_vec()
.extend_from_slice(&self.inline[..old_len]);
}
}
}

Expand Down Expand Up @@ -592,4 +602,4 @@ where
fn to_smolstr(&self) -> SmolStr {
format_smolstr!("{}", self)
}
}
}
2 changes: 1 addition & 1 deletion tests/test.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use proptest::{prop_assert, prop_assert_eq, proptest};

use smol_str::{SmolStr, format_smolstr};
use smol_str::{format_smolstr, SmolStr};

#[test]
#[cfg(target_pointer_width = "64")]
Expand Down

0 comments on commit d3d5cb8

Please sign in to comment.