-
Notifications
You must be signed in to change notification settings - Fork 54
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
feat: Add SmolStr::from_static
#62
Conversation
README.md
Outdated
@@ -7,13 +7,14 @@ | |||
|
|||
A `SmolStr` is a string type that has the following properties: | |||
|
|||
* `size_of::<SmolStr>() == 24 (therefore == size_of::<String>() on 64 bit platforms) | |||
* `size_of::<SmolStr>() == 24` (therefor `== size_of::<String>()` on 64 bit platforms) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* `size_of::<SmolStr>() == 24` (therefor `== size_of::<String>()` on 64 bit platforms) | |
* `size_of::<SmolStr>() == 24` (therefore `== size_of::<String>()` on 64 bit platforms) |
README.md
Outdated
* `Clone` is `O(1)` | ||
* Strings are stack-allocated if they are: | ||
* Up to 23 bytes long | ||
* Longer than 23 bytes, but substrings of `WS` (see `src/lib.rs`). Such strings consist | ||
solely of consecutive newlines, followed by consecutive spaces | ||
* If a string does not satisfy the aforementioned conditions, it is heap-allocated | ||
* Additionally, a `SmolStr` can be explicitely created from a `&'static str` without allocation |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* Additionally, a `SmolStr` can be explicitely created from a `&'static str` without allocation | |
* Additionally, a `SmolStr` can be explicitly created from a `&'static str` without allocation |
src/lib.rs
Outdated
@@ -78,6 +79,17 @@ impl SmolStr { | |||
}) | |||
} | |||
|
|||
/// Constructs a `SmolStr` from a statically allocated buffer. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// Constructs a `SmolStr` from a statically allocated buffer. | |
/// Constructs a `SmolStr` from a statically allocated string. |
Thanks, should probably bump the version in the manifest too. |
Allows creating `SmolStr`s longer than 23 bytes in constant contexts. This is done by replacing the `Repr::Substring` variant by a more general `Repr::Static(&'static str)` variant, and borrowing from ̀`WS` directly instead of storing two `usize`s. As a bonus, it also simplifies the `as_str` implementation, hopefully saving an extra branch.
Thanks! Will make a release for this later, depending on whether other PRs will be merged or not. |
Allows creating
SmolStr
s longer than 23 bytes in constant contexts.This is done by replacing the
Repr::Substring
variant by a more generalRepr::Static(&'static str)
variant, and borrowing from ̀WS
directly instead of storing twousize
s.As a bonus, it also simplifies the
as_str
implementation, hopefully saving an extra branch.