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

feat: Add SmolStr::from_static #62

Merged
merged 1 commit into from
Jan 11, 2024
Merged

Conversation

moulins
Copy link
Contributor

@moulins moulins commented Jan 2, 2024

Allows creating SmolStrs 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 usizes.

As a bonus, it also simplifies the as_str implementation, hopefully saving an extra branch.

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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* `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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* 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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// Constructs a `SmolStr` from a statically allocated buffer.
/// Constructs a `SmolStr` from a statically allocated string.

@lnicola
Copy link
Member

lnicola commented Jan 2, 2024

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.
@Veykril Veykril merged commit f8ed961 into rust-analyzer:master Jan 11, 2024
1 check passed
@Veykril
Copy link
Member

Veykril commented Jan 11, 2024

Thanks! Will make a release for this later, depending on whether other PRs will be merged or not.

@moulins moulins deleted the static-str branch January 14, 2024 06:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants