Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Manually implement Clone #61

Merged
merged 5 commits into from
Jan 31, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,18 @@ use core::{
/// Note that a specialized interner might be a better solution for some use cases.
///
/// `WS`: A string of 32 newlines followed by 128 spaces.
#[derive(Clone)]
pub struct SmolStr(Repr);

impl Clone for SmolStr {
#[inline]
fn clone(&self) -> Self {
if !self.is_heap_allocated() {
return unsafe { core::ptr::read(self as *const SmolStr) };
}
Self(self.0.clone())
}
}

impl SmolStr {
#[deprecated = "Use `new_inline` instead"]
pub const fn new_inline_from_ascii(len: usize, bytes: &[u8]) -> SmolStr {
Expand Down Expand Up @@ -446,12 +455,12 @@ enum InlineSize {

#[derive(Clone, Debug)]
enum Repr {
Heap(Arc<str>),
Static(&'static str),
Inline {
len: InlineSize,
buf: [u8; INLINE_CAP],
},
Static(&'static str),
Heap(Arc<str>),
}

impl Repr {
Expand Down
Loading