smol_str-0.2.0/.cargo_vcs_info.json0000644000000001360000000000100126750ustar { "git": { "sha1": "f927bdbfc8a60d714948b5d730976bcd5793816b" }, "path_in_vcs": "" }smol_str-0.2.0/.github/ci.rs000064400000000000000000000063111046102023000137670ustar 00000000000000use std::{ env, fs, process::{self, Command, ExitStatus, Stdio}, time::Instant, }; type Error = Box; type Result = std::result::Result; fn main() { if let Err(err) = try_main() { eprintln!("{}", err); process::exit(1); } } fn try_main() -> Result<()> { let cwd = env::current_dir()?; let cargo_toml = cwd.join("Cargo.toml"); assert!( cargo_toml.exists(), "Cargo.toml not found, cwd: {}", cwd.display() ); { let _s = Section::new("BUILD_NO_DEFAULT_FEATURES"); shell("cargo test --all-features --workspace --no-run --no-default-features")?; } { let _s = Section::new("BUILD"); shell("cargo test --all-features --workspace --no-run")?; } { let _s = Section::new("TEST"); shell("cargo test --all-features --workspace")?; shell("cargo test --no-default-features --workspace")?; } let current_branch = shell_output("git branch --show-current")?; if ¤t_branch == "master" { let _s = Section::new("PUBLISH"); let manifest = fs::read_to_string(&cargo_toml)?; let version = get_field(&manifest, "version")?; let tag = format!("v{}", version); let tags = shell_output("git tag --list")?; if !tags.contains(&tag) { let token = env::var("CRATES_IO_TOKEN").unwrap(); shell(&format!("git tag v{}", version))?; shell(&format!("cargo publish --token {}", token))?; shell("git push --tags")?; } } Ok(()) } fn get_field<'a>(text: &'a str, name: &str) -> Result<&'a str> { for line in text.lines() { let words = line.split_ascii_whitespace().collect::>(); match words.as_slice() { [n, "=", v, ..] if n.trim() == name => { assert!(v.starts_with('"') && v.ends_with('"')); return Ok(&v[1..v.len() - 1]); } _ => (), } } Err(format!("can't find `{}` in\n----\n{}\n----\n", name, text))? } fn shell(cmd: &str) -> Result<()> { let status = command(cmd).status()?; check_status(status) } fn shell_output(cmd: &str) -> Result { let output = command(cmd).stderr(Stdio::inherit()).output()?; check_status(output.status)?; let res = String::from_utf8(output.stdout)?; let res = res.trim().to_string(); println!("{}", res); Ok(res) } fn command(cmd: &str) -> Command { eprintln!("> {}", cmd); let words = cmd.split_ascii_whitespace().collect::>(); let (cmd, args) = words.split_first().unwrap(); let mut res = Command::new(cmd); res.args(args); res } fn check_status(status: ExitStatus) -> Result<()> { if !status.success() { Err(format!("$status: {}", status))?; } Ok(()) } struct Section { name: &'static str, start: Instant, } impl Section { fn new(name: &'static str) -> Section { println!("::group::{}", name); let start = Instant::now(); Section { name, start } } } impl Drop for Section { fn drop(&mut self) { eprintln!("{}: {:.2?}", self.name, self.start.elapsed()); println!("::endgroup::"); } } smol_str-0.2.0/.github/workflows/ci.yaml000064400000000000000000000012231046102023000163370ustar 00000000000000name: CI on: pull_request: push: branches: - master - staging - trying env: CARGO_INCREMENTAL: 0 CARGO_NET_RETRY: 10 CI: 1 RUST_BACKTRACE: short RUSTFLAGS: -D warnings RUSTUP_MAX_RETRIES: 10 jobs: rust: name: Rust runs-on: ubuntu-latest steps: - name: Checkout repository uses: actions/checkout@v2 with: fetch-depth: 0 - name: Install Rust toolchain uses: actions-rs/toolchain@v1 with: toolchain: stable profile: minimal override: true - run: rustc ./.github/ci.rs && ./ci env: CRATES_IO_TOKEN: ${{ secrets.CRATES_IO_TOKEN }} smol_str-0.2.0/.gitignore000064400000000000000000000000261046102023000134530ustar 00000000000000/target /ci Cargo.locksmol_str-0.2.0/Cargo.toml0000644000000021400000000000100106700ustar # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies. # # If you are reading this file be aware that the original Cargo.toml # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. [package] edition = "2018" name = "smol_str" version = "0.2.0" authors = ["Aleksey Kladov "] description = "small-string optimized string type with O(1) clone" readme = "README.md" license = "MIT OR Apache-2.0" repository = "https://github.com/rust-analyzer/smol_str" [dependencies.arbitrary] version = "1.1.0" optional = true [dependencies.serde] version = "1.0.136" optional = true default_features = false [dev-dependencies.proptest] version = "1.0.0" [dev-dependencies.serde] version = "1.0.136" features = ["derive"] [dev-dependencies.serde_json] version = "1.0.79" [features] default = ["std"] std = ["serde?/std"] smol_str-0.2.0/Cargo.toml.orig000064400000000000000000000011061046102023000143520ustar 00000000000000[package] name = "smol_str" version = "0.2.0" description = "small-string optimized string type with O(1) clone" license = "MIT OR Apache-2.0" repository = "https://github.com/rust-analyzer/smol_str" authors = ["Aleksey Kladov "] edition = "2018" [dependencies] serde = { version = "1.0.136", optional = true, default_features = false } arbitrary = { version = "1.1.0", optional = true } [dev-dependencies] proptest = "1.0.0" serde_json = "1.0.79" serde = { version = "1.0.136", features = ["derive"] } [features] default = ["std"] std = ["serde?/std"] smol_str-0.2.0/LICENSE-APACHE000064400000000000000000000251371046102023000134210ustar 00000000000000 Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. smol_str-0.2.0/LICENSE-MIT000064400000000000000000000017771046102023000131350ustar 00000000000000Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. smol_str-0.2.0/README.md000064400000000000000000000025021046102023000127430ustar 00000000000000# smol_str [![CI](https://github.com/rust-analyzer/smol_str/workflows/CI/badge.svg)](https://github.com/rust-analyzer/smol_str/actions?query=branch%3Amaster+workflow%3ACI) [![Crates.io](https://img.shields.io/crates/v/smol_str.svg)](https://crates.io/crates/smol_str) [![API reference](https://docs.rs/smol_str/badge.svg)](https://docs.rs/smol_str/) A `SmolStr` is a string type that has the following properties: * `size_of::() == 24 (therefore == size_of::() on 64 bit platforms) * `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 Unlike `String`, however, `SmolStr` is immutable. The primary use case for `SmolStr` is a good enough default storage for tokens of typical programming languages. Strings consisting of a series of newlines, followed by a series of whitespace are a typical pattern in computer programs because of indentation. Note that a specialized interner might be a better solution for some use cases. ## MSRV Policy Minimal Supported Rust Version: latest stable. Bumping MSRV is not considered a semver-breaking change. smol_str-0.2.0/bors.toml000064400000000000000000000000621046102023000133250ustar 00000000000000status = [ "Rust" ] delete_merged_branches = true smol_str-0.2.0/src/lib.rs000064400000000000000000000361061046102023000133760ustar 00000000000000#![no_std] extern crate alloc; use alloc::{ borrow::Cow, boxed::Box, string::{String, ToString}, sync::Arc, }; use core::{ borrow::Borrow, cmp::{self, Ordering}, convert::Infallible, fmt, hash, iter, mem::transmute, ops::Deref, str::FromStr, }; /// A `SmolStr` is a string type that has the following properties: /// /// * `size_of::() == 24 (therefor == size_of::() on 64 bit platforms) /// * `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 below). Such strings consist /// solely of consecutive newlines, followed by consecutive spaces /// * If a string does not satisfy the aforementioned conditions, it is heap-allocated /// /// Unlike `String`, however, `SmolStr` is immutable. The primary use case for /// `SmolStr` is a good enough default storage for tokens of typical programming /// languages. Strings consisting of a series of newlines, followed by a series of /// whitespace are a typical pattern in computer programs because of indentation. /// 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 SmolStr { #[deprecated = "Use `new_inline` instead"] pub const fn new_inline_from_ascii(len: usize, bytes: &[u8]) -> SmolStr { let _len_is_short = [(); INLINE_CAP + 1][len]; const ZEROS: &[u8] = &[0; INLINE_CAP]; let mut buf = [0; INLINE_CAP]; macro_rules! s { ($($idx:literal),*) => ( $(s!(set $idx);)* ); (set $idx:literal) => ({ let src: &[u8] = [ZEROS, bytes][($idx < len) as usize]; let byte = src[$idx]; let _is_ascii = [(); 128][byte as usize]; buf[$idx] = byte }); } s!(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22); SmolStr(Repr::Inline { len: unsafe { transmute(len as u8) }, buf, }) } /// Constructs inline variant of `SmolStr`. /// /// Panics if `text.len() > 23`. #[inline] pub const fn new_inline(text: &str) -> SmolStr { let mut buf = [0; INLINE_CAP]; let mut i = 0; while i < text.len() { buf[i] = text.as_bytes()[i]; i += 1 } SmolStr(Repr::Inline { len: unsafe { transmute(text.len() as u8) }, buf, }) } pub fn new(text: T) -> SmolStr where T: AsRef, { SmolStr(Repr::new(text)) } #[inline(always)] pub fn as_str(&self) -> &str { self.0.as_str() } #[inline(always)] pub fn to_string(&self) -> String { self.as_str().to_string() } #[inline(always)] pub fn len(&self) -> usize { self.0.len() } #[inline(always)] pub fn is_empty(&self) -> bool { self.0.is_empty() } #[inline(always)] pub fn is_heap_allocated(&self) -> bool { match self.0 { Repr::Heap(..) => true, _ => false, } } fn from_char_iter>(mut iter: I) -> SmolStr { let (min_size, _) = iter.size_hint(); if min_size > INLINE_CAP { let heap: String = iter.collect(); return SmolStr(Repr::Heap(heap.into_boxed_str().into())); } let mut len = 0; let mut buf = [0u8; INLINE_CAP]; while let Some(ch) = iter.next() { let size = ch.len_utf8(); if size + len > INLINE_CAP { let (min_remaining, _) = iter.size_hint(); let mut heap = String::with_capacity(size + len + min_remaining); heap.push_str(core::str::from_utf8(&buf[..len]).unwrap()); heap.push(ch); heap.extend(iter); return SmolStr(Repr::Heap(heap.into_boxed_str().into())); } ch.encode_utf8(&mut buf[len..]); len += size; } SmolStr(Repr::Inline { len: unsafe { transmute(len as u8) }, buf, }) } } impl Default for SmolStr { fn default() -> SmolStr { SmolStr::new("") } } impl Deref for SmolStr { type Target = str; fn deref(&self) -> &str { self.as_str() } } impl PartialEq for SmolStr { fn eq(&self, other: &SmolStr) -> bool { self.as_str() == other.as_str() } } impl Eq for SmolStr {} impl PartialEq for SmolStr { fn eq(&self, other: &str) -> bool { self.as_str() == other } } impl PartialEq for str { fn eq(&self, other: &SmolStr) -> bool { other == self } } impl<'a> PartialEq<&'a str> for SmolStr { fn eq(&self, other: &&'a str) -> bool { self == *other } } impl<'a> PartialEq for &'a str { fn eq(&self, other: &SmolStr) -> bool { *self == other } } impl PartialEq for SmolStr { fn eq(&self, other: &String) -> bool { self.as_str() == other } } impl PartialEq for String { fn eq(&self, other: &SmolStr) -> bool { other == self } } impl<'a> PartialEq<&'a String> for SmolStr { fn eq(&self, other: &&'a String) -> bool { self == *other } } impl<'a> PartialEq for &'a String { fn eq(&self, other: &SmolStr) -> bool { *self == other } } impl Ord for SmolStr { fn cmp(&self, other: &SmolStr) -> Ordering { self.as_str().cmp(other.as_str()) } } impl PartialOrd for SmolStr { fn partial_cmp(&self, other: &SmolStr) -> Option { Some(self.cmp(other)) } } impl hash::Hash for SmolStr { fn hash(&self, hasher: &mut H) { self.as_str().hash(hasher) } } impl fmt::Debug for SmolStr { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::Debug::fmt(self.as_str(), f) } } impl fmt::Display for SmolStr { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::Display::fmt(self.as_str(), f) } } impl iter::FromIterator for SmolStr { fn from_iter>(iter: I) -> SmolStr { let iter = iter.into_iter(); Self::from_char_iter(iter) } } fn build_from_str_iter(mut iter: impl Iterator) -> SmolStr where T: AsRef, String: iter::Extend, { let mut len = 0; let mut buf = [0u8; INLINE_CAP]; while let Some(slice) = iter.next() { let slice = slice.as_ref(); let size = slice.len(); if size + len > INLINE_CAP { let mut heap = String::with_capacity(size + len); heap.push_str(core::str::from_utf8(&buf[..len]).unwrap()); heap.push_str(&slice); heap.extend(iter); return SmolStr(Repr::Heap(heap.into_boxed_str().into())); } (&mut buf[len..][..size]).copy_from_slice(slice.as_bytes()); len += size; } SmolStr(Repr::Inline { len: unsafe { transmute(len as u8) }, buf, }) } impl iter::FromIterator for SmolStr { fn from_iter>(iter: I) -> SmolStr { build_from_str_iter(iter.into_iter()) } } impl<'a> iter::FromIterator<&'a String> for SmolStr { fn from_iter>(iter: I) -> SmolStr { SmolStr::from_iter(iter.into_iter().map(|x| x.as_str())) } } impl<'a> iter::FromIterator<&'a str> for SmolStr { fn from_iter>(iter: I) -> SmolStr { build_from_str_iter(iter.into_iter()) } } impl AsRef for SmolStr { #[inline(always)] fn as_ref(&self) -> &str { self.as_str() } } impl From<&str> for SmolStr { #[inline] fn from(s: &str) -> SmolStr { SmolStr::new(s) } } impl From<&mut str> for SmolStr { #[inline] fn from(s: &mut str) -> SmolStr { SmolStr::new(s) } } impl From<&String> for SmolStr { #[inline] fn from(s: &String) -> SmolStr { SmolStr::new(s) } } impl From for SmolStr { #[inline(always)] fn from(text: String) -> Self { Self::new(text) } } impl From> for SmolStr { #[inline] fn from(s: Box) -> SmolStr { SmolStr::new(s) } } impl<'a> From> for SmolStr { #[inline] fn from(s: Cow<'a, str>) -> SmolStr { SmolStr::new(s) } } impl From for String { #[inline(always)] fn from(text: SmolStr) -> Self { text.as_str().into() } } impl Borrow for SmolStr { #[inline(always)] fn borrow(&self) -> &str { self.as_str() } } impl FromStr for SmolStr { type Err = Infallible; #[inline] fn from_str(s: &str) -> Result { Ok(SmolStr::from(s)) } } #[cfg(feature = "arbitrary")] impl<'a> arbitrary::Arbitrary<'a> for SmolStr { fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> Result { let s = <&str>::arbitrary(u)?; Ok(SmolStr::new(s)) } } const INLINE_CAP: usize = 23; const N_NEWLINES: usize = 32; const N_SPACES: usize = 128; const WS: &str = "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n "; #[derive(Clone, Copy, Debug)] #[repr(u8)] enum InlineSize { _V0 = 0, _V1, _V2, _V3, _V4, _V5, _V6, _V7, _V8, _V9, _V10, _V11, _V12, _V13, _V14, _V15, _V16, _V17, _V18, _V19, _V20, _V21, _V22, _V23, } #[derive(Clone, Debug)] enum Repr { Heap(Arc), Inline { len: InlineSize, buf: [u8; INLINE_CAP], }, Substring { newlines: usize, spaces: usize, }, } impl Repr { fn new(text: T) -> Self where T: AsRef, { { let text = text.as_ref(); let len = text.len(); if len <= INLINE_CAP { let mut buf = [0; INLINE_CAP]; buf[..len].copy_from_slice(text.as_bytes()); return Repr::Inline { len: unsafe { transmute(len as u8) }, buf, }; } if len <= N_NEWLINES + N_SPACES { let bytes = text.as_bytes(); let possible_newline_count = cmp::min(len, N_NEWLINES); let newlines = bytes[..possible_newline_count] .iter() .take_while(|&&b| b == b'\n') .count(); let possible_space_count = len - newlines; if possible_space_count <= N_SPACES && bytes[newlines..].iter().all(|&b| b == b' ') { let spaces = possible_space_count; return Repr::Substring { newlines, spaces }; } } } Repr::Heap(text.as_ref().into()) } #[inline(always)] fn len(&self) -> usize { match self { Repr::Heap(data) => data.len(), Repr::Inline { len, .. } => *len as usize, Repr::Substring { newlines, spaces } => *newlines + *spaces, } } #[inline(always)] fn is_empty(&self) -> bool { match self { Repr::Heap(data) => data.is_empty(), Repr::Inline { len, .. } => *len as u8 == 0, // A substring isn't created for an empty string. Repr::Substring { .. } => false, } } #[inline] fn as_str(&self) -> &str { match self { Repr::Heap(data) => &*data, Repr::Inline { len, buf } => { let len = *len as usize; let buf = &buf[..len]; unsafe { ::core::str::from_utf8_unchecked(buf) } } Repr::Substring { newlines, spaces } => { let newlines = *newlines; let spaces = *spaces; assert!(newlines <= N_NEWLINES && spaces <= N_SPACES); &WS[N_NEWLINES - newlines..N_NEWLINES + spaces] } } } } #[cfg(feature = "serde")] mod serde { use alloc::{string::String, vec::Vec}; use core::fmt; use serde::de::{Deserializer, Error, Unexpected, Visitor}; use crate::SmolStr; // https://github.com/serde-rs/serde/blob/629802f2abfd1a54a6072992888fea7ca5bc209f/serde/src/private/de.rs#L56-L125 fn smol_str<'de: 'a, 'a, D>(deserializer: D) -> Result where D: Deserializer<'de>, { struct SmolStrVisitor; impl<'a> Visitor<'a> for SmolStrVisitor { type Value = SmolStr; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter.write_str("a string") } fn visit_str(self, v: &str) -> Result where E: Error, { Ok(SmolStr::from(v)) } fn visit_borrowed_str(self, v: &'a str) -> Result where E: Error, { Ok(SmolStr::from(v)) } fn visit_string(self, v: String) -> Result where E: Error, { Ok(SmolStr::from(v)) } fn visit_bytes(self, v: &[u8]) -> Result where E: Error, { match core::str::from_utf8(v) { Ok(s) => Ok(SmolStr::from(s)), Err(_) => Err(Error::invalid_value(Unexpected::Bytes(v), &self)), } } fn visit_borrowed_bytes(self, v: &'a [u8]) -> Result where E: Error, { match core::str::from_utf8(v) { Ok(s) => Ok(SmolStr::from(s)), Err(_) => Err(Error::invalid_value(Unexpected::Bytes(v), &self)), } } fn visit_byte_buf(self, v: Vec) -> Result where E: Error, { match String::from_utf8(v) { Ok(s) => Ok(SmolStr::from(s)), Err(e) => Err(Error::invalid_value( Unexpected::Bytes(&e.into_bytes()), &self, )), } } } deserializer.deserialize_str(SmolStrVisitor) } impl serde::Serialize for SmolStr { fn serialize(&self, serializer: S) -> Result where S: serde::Serializer, { self.as_str().serialize(serializer) } } impl<'de> serde::Deserialize<'de> for SmolStr { fn deserialize(deserializer: D) -> Result where D: serde::Deserializer<'de>, { smol_str(deserializer) } } } smol_str-0.2.0/tests/test.rs000064400000000000000000000176031046102023000141630ustar 00000000000000use proptest::{prop_assert, prop_assert_eq, proptest}; use smol_str::SmolStr; #[test] #[cfg(target_pointer_width = "64")] fn smol_str_is_smol() { assert_eq!( ::std::mem::size_of::(), ::std::mem::size_of::(), ); } #[test] fn assert_traits() { fn f() {} f::(); } #[test] fn conversions() { let s: SmolStr = "Hello, World!".into(); let s: String = s.into(); assert_eq!(s, "Hello, World!") } #[test] fn const_fn_ctor() { const EMPTY: SmolStr = SmolStr::new_inline(""); const A: SmolStr = SmolStr::new_inline("A"); const HELLO: SmolStr = SmolStr::new_inline("HELLO"); const LONG: SmolStr = SmolStr::new_inline("ABCDEFGHIZKLMNOPQRSTUVW"); assert_eq!(EMPTY, SmolStr::from("")); assert_eq!(A, SmolStr::from("A")); assert_eq!(HELLO, SmolStr::from("HELLO")); assert_eq!(LONG, SmolStr::from("ABCDEFGHIZKLMNOPQRSTUVW")); } #[allow(deprecated)] #[test] fn old_const_fn_ctor() { const EMPTY: SmolStr = SmolStr::new_inline_from_ascii(0, b""); const A: SmolStr = SmolStr::new_inline_from_ascii(1, b"A"); const HELLO: SmolStr = SmolStr::new_inline_from_ascii(5, b"HELLO"); const LONG: SmolStr = SmolStr::new_inline_from_ascii(23, b"ABCDEFGHIZKLMNOPQRSTUVW"); assert_eq!(EMPTY, SmolStr::from("")); assert_eq!(A, SmolStr::from("A")); assert_eq!(HELLO, SmolStr::from("HELLO")); assert_eq!(LONG, SmolStr::from("ABCDEFGHIZKLMNOPQRSTUVW")); } fn check_props(std_str: &str, smol: SmolStr) -> Result<(), proptest::test_runner::TestCaseError> { prop_assert_eq!(smol.as_str(), std_str); prop_assert_eq!(smol.len(), std_str.len()); prop_assert_eq!(smol.is_empty(), std_str.is_empty()); if smol.len() <= 23 { prop_assert!(!smol.is_heap_allocated()); } Ok(()) } proptest! { #[test] fn roundtrip(s: String) { check_props(s.as_str(), SmolStr::new(s.clone()))?; } #[test] fn roundtrip_spaces(s in r"( )*") { check_props(s.as_str(), SmolStr::new(s.clone()))?; } #[test] fn roundtrip_newlines(s in r"\n*") { check_props(s.as_str(), SmolStr::new(s.clone()))?; } #[test] fn roundtrip_ws(s in r"( |\n)*") { check_props(s.as_str(), SmolStr::new(s.clone()))?; } #[test] fn from_string_iter(slices in proptest::collection::vec(".*", 1..100)) { let string: String = slices.iter().map(|x| x.as_str()).collect(); let smol: SmolStr = slices.into_iter().collect(); check_props(string.as_str(), smol)?; } #[test] fn from_str_iter(slices in proptest::collection::vec(".*", 1..100)) { let string: String = slices.iter().map(|x| x.as_str()).collect(); let smol: SmolStr = slices.iter().collect(); check_props(string.as_str(), smol)?; } } #[cfg(feature = "serde")] mod serde_tests { use super::*; use serde::{Deserialize, Serialize}; use std::collections::HashMap; #[derive(Serialize, Deserialize)] struct SmolStrStruct { pub(crate) s: SmolStr, pub(crate) vec: Vec, pub(crate) map: HashMap, } #[test] fn test_serde() { let s = SmolStr::new("Hello, World"); let s = serde_json::to_string(&s).unwrap(); assert_eq!(s, "\"Hello, World\""); let s: SmolStr = serde_json::from_str(&s).unwrap(); assert_eq!(s, "Hello, World"); } #[test] fn test_serde_reader() { let s = SmolStr::new("Hello, World"); let s = serde_json::to_string(&s).unwrap(); assert_eq!(s, "\"Hello, World\""); let s: SmolStr = serde_json::from_reader(std::io::Cursor::new(s)).unwrap(); assert_eq!(s, "Hello, World"); } #[test] fn test_serde_struct() { let mut map = HashMap::new(); map.insert(SmolStr::new("a"), SmolStr::new("ohno")); let struct_ = SmolStrStruct { s: SmolStr::new("Hello, World"), vec: vec![SmolStr::new("Hello, World"), SmolStr::new("Hello, World")], map, }; let s = serde_json::to_string(&struct_).unwrap(); let _new_struct: SmolStrStruct = serde_json::from_str(&s).unwrap(); } #[test] fn test_serde_struct_reader() { let mut map = HashMap::new(); map.insert(SmolStr::new("a"), SmolStr::new("ohno")); let struct_ = SmolStrStruct { s: SmolStr::new("Hello, World"), vec: vec![SmolStr::new("Hello, World"), SmolStr::new("Hello, World")], map, }; let s = serde_json::to_string(&struct_).unwrap(); let _new_struct: SmolStrStruct = serde_json::from_reader(std::io::Cursor::new(s)).unwrap(); } #[test] fn test_serde_hashmap() { let mut map = HashMap::new(); map.insert(SmolStr::new("a"), SmolStr::new("ohno")); let s = serde_json::to_string(&map).unwrap(); let _s: HashMap = serde_json::from_str(&s).unwrap(); } #[test] fn test_serde_hashmap_reader() { let mut map = HashMap::new(); map.insert(SmolStr::new("a"), SmolStr::new("ohno")); let s = serde_json::to_string(&map).unwrap(); let _s: HashMap = serde_json::from_reader(std::io::Cursor::new(s)).unwrap(); } #[test] fn test_serde_vec() { let vec = vec![SmolStr::new(""), SmolStr::new("b")]; let s = serde_json::to_string(&vec).unwrap(); let _s: Vec = serde_json::from_str(&s).unwrap(); } #[test] fn test_serde_vec_reader() { let vec = vec![SmolStr::new(""), SmolStr::new("b")]; let s = serde_json::to_string(&vec).unwrap(); let _s: Vec = serde_json::from_reader(std::io::Cursor::new(s)).unwrap(); } } #[test] fn test_search_in_hashmap() { let mut m = ::std::collections::HashMap::::new(); m.insert("aaa".into(), 17); assert_eq!(17, *m.get("aaa").unwrap()); } #[test] fn test_from_char_iterator() { let examples = [ // Simple keyword-like strings ("if", false), ("for", false), ("impl", false), // Strings containing two-byte characters ("パーティーへ行かないか", true), ("パーティーへ行か", true), ("パーティーへ行_", false), ("和製漢語", false), ("部落格", false), ("사회과학원 어학연구소", true), // String containing diverse characters ("表ポあA鷗ŒéB逍Üߪąñ丂㐀𠀀", true), ]; for (raw, is_heap) in &examples { let s: SmolStr = raw.chars().collect(); assert_eq!(s.as_str(), *raw); assert_eq!(s.is_heap_allocated(), *is_heap); } // String which has too many characters to even consider inlining: Chars::size_hint uses // (`len` + 3) / 4. With `len` = 89, this results in 23, so `from_iter` will immediately // heap allocate let raw: String = std::iter::repeat('a').take(23 * 4 + 1).collect(); let s: SmolStr = raw.chars().collect(); assert_eq!(s.as_str(), raw); assert!(s.is_heap_allocated()); } #[test] fn test_bad_size_hint_char_iter() { struct BadSizeHint(I); impl> Iterator for BadSizeHint { type Item = T; fn next(&mut self) -> Option { self.0.next() } fn size_hint(&self) -> (usize, Option) { (1024, None) } } let data = "testing"; let collected: SmolStr = BadSizeHint(data.chars()).collect(); let new = SmolStr::new(data); // Because of the bad size hint, `collected` will be heap allocated, but `new` will be inline // If we try to use the type of the string (inline/heap) to quickly test for equality, we need to ensure // `collected` is inline allocated instead assert!(collected.is_heap_allocated()); assert!(!new.is_heap_allocated()); assert_eq!(new, collected); } smol_str-0.2.0/tests/tidy.rs000064400000000000000000000024221046102023000141460ustar 00000000000000use std::{ env, path::{Path, PathBuf}, process::{Command, Stdio}, }; fn project_root() -> PathBuf { PathBuf::from( env::var("CARGO_MANIFEST_DIR").unwrap_or_else(|_| env!("CARGO_MANIFEST_DIR").to_owned()), ) } fn run(cmd: &str, dir: impl AsRef) -> Result<(), ()> { let mut args: Vec<_> = cmd.split_whitespace().collect(); let bin = args.remove(0); println!("> {}", cmd); let output = Command::new(bin) .args(args) .current_dir(dir) .stdin(Stdio::null()) .stdout(Stdio::piped()) .stderr(Stdio::inherit()) .output() .map_err(drop)?; if output.status.success() { Ok(()) } else { let stdout = String::from_utf8(output.stdout).map_err(drop)?; print!("{}", stdout); Err(()) } } #[test] fn check_code_formatting() { let dir = project_root(); if run("rustfmt +stable --version", &dir).is_err() { panic!( "failed to run rustfmt from toolchain 'stable'; \ please run `rustup component add rustfmt --toolchain stable` to install it.", ); } if run("cargo +stable fmt -- --check", &dir).is_err() { panic!("code is not properly formatted; please format the code by running `cargo fmt`") } }