Vendor dependencies for 0.3.0 release

This commit is contained in:
2025-09-27 10:29:08 -05:00
parent 0c8d39d483
commit 82ab7f317b
26803 changed files with 16134934 additions and 0 deletions

1
vendor/smol_str/.cargo-checksum.json vendored Normal file
View File

@@ -0,0 +1 @@
{"files":{"Cargo.toml":"ddbc0a2fbeed9eb84ac10f9dd2c7a4118dbc123e0d806df831de517588be1557","LICENSE-APACHE":"a60eea817514531668d7e00765731449fe14d059d3249e0bc93b36de45f759f2","LICENSE-MIT":"23f18e03dc49df91622fe2a76176497404e46ced8a715d9d2b67a7446571cca3","README.md":"27bf5ba8c9132c77a1287466d3c92d89b9789081da32396dd33cfdee5c3ed806","bors.toml":"ebd69f714a49dceb8fd10ebadfea6e2767be4732fdef49eddf6239151b4bc78c","src/lib.rs":"82e51ecc01f7fd5cb14c308c00fec23d5b993ab22f684ba9313792f6c2b49dbf","tests/test.rs":"a028134051b34923866c64a4b897e748cc0eeb583f378f074f37b2465062801a","tests/tidy.rs":"b86cc626ca3315d54ae3e93c7be6fd6e7065d399c3c6b786bdfe30789f6b5cf7"},"package":"dd538fb6910ac1099850255cf94a94df6551fbdd602454387d0adb2d1ca6dead"}

43
vendor/smol_str/Cargo.toml vendored Normal file
View File

@@ -0,0 +1,43 @@
# 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.2"
authors = ["Aleksey Kladov <aleksey.kladov@gmail.com>"]
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"]

201
vendor/smol_str/LICENSE-APACHE vendored Normal file
View File

@@ -0,0 +1,201 @@
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.

23
vendor/smol_str/LICENSE-MIT vendored Normal file
View File

@@ -0,0 +1,23 @@
Permission 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.

29
vendor/smol_str/README.md vendored Normal file
View File

@@ -0,0 +1,29 @@
# 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::<SmolStr>() == 24` (therefore `== size_of::<String>()` 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
* Additionally, a `SmolStr` can be explicitly created from a `&'static str` without allocation
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.

2
vendor/smol_str/bors.toml vendored Normal file
View File

@@ -0,0 +1,2 @@
status = [ "Rust" ]
delete_merged_branches = true

867
vendor/smol_str/src/lib.rs vendored Normal file
View File

@@ -0,0 +1,867 @@
#![no_std]
extern crate alloc;
use alloc::{borrow::Cow, boxed::Box, string::String, 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::<SmolStr>() == 24` (therefor `== size_of::<String>()` 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
/// * Additionally, a `SmolStr` can be explicitely created from a `&'static str` without allocation
///
/// 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.
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 {
assert!(len <= INLINE_CAP);
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 {
// SAFETY: We know that `len` is less than or equal to the maximum value of `InlineSize`
// as we asserted it.
len: unsafe { InlineSize::transmute_from_u8(len as u8) },
buf,
})
}
/// Constructs inline variant of `SmolStr`.
///
/// Panics if `text.len() > 23`.
#[inline]
pub const fn new_inline(text: &str) -> SmolStr {
assert!(text.len() <= INLINE_CAP); // avoids checks in loop
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 {
// SAFETY: We know that `len` is less than or equal to the maximum value of `InlineSize`
// as we asserted it.
len: unsafe { InlineSize::transmute_from_u8(text.len() as u8) },
buf,
})
}
/// Constructs a `SmolStr` from a statically allocated string.
///
/// This never allocates.
#[inline(always)]
pub const fn new_static(text: &'static str) -> SmolStr {
// NOTE: this never uses the inline storage; if a canonical
// representation is needed, we could check for `len() < INLINE_CAP`
// and call `new_inline`, but this would mean an extra branch.
SmolStr(Repr::Static(text))
}
pub fn new<T>(text: T) -> SmolStr
where
T: AsRef<str>,
{
SmolStr(Repr::new(text))
}
#[inline(always)]
pub fn as_str(&self) -> &str {
self.0.as_str()
}
#[allow(clippy::inherent_to_string_shadow_display)]
#[inline(always)]
pub fn to_string(&self) -> String {
use alloc::borrow::ToOwned;
self.as_str().to_owned()
}
#[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 const fn is_heap_allocated(&self) -> bool {
matches!(self.0, Repr::Heap(..))
}
fn from_char_iter<I: iter::Iterator<Item = char>>(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 {
// SAFETY: We know that `len` is less than or equal to the maximum value of `InlineSize`
// as we otherwise return early.
len: unsafe { InlineSize::transmute_from_u8(len as u8) },
buf,
})
}
}
impl Default for SmolStr {
#[inline(always)]
fn default() -> SmolStr {
SmolStr(Repr::Inline {
len: InlineSize::_V0,
buf: [0; INLINE_CAP],
})
}
}
impl Deref for SmolStr {
type Target = str;
#[inline(always)]
fn deref(&self) -> &str {
self.as_str()
}
}
impl PartialEq<SmolStr> for SmolStr {
fn eq(&self, other: &SmolStr) -> bool {
self.0.ptr_eq(&other.0) || self.as_str() == other.as_str()
}
}
impl Eq for SmolStr {}
impl PartialEq<str> for SmolStr {
fn eq(&self, other: &str) -> bool {
self.as_str() == other
}
}
impl PartialEq<SmolStr> 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<SmolStr> for &'a str {
fn eq(&self, other: &SmolStr) -> bool {
*self == other
}
}
impl PartialEq<String> for SmolStr {
fn eq(&self, other: &String) -> bool {
self.as_str() == other
}
}
impl PartialEq<SmolStr> 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<SmolStr> 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<Ordering> {
Some(self.cmp(other))
}
}
impl hash::Hash for SmolStr {
fn hash<H: hash::Hasher>(&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<char> for SmolStr {
fn from_iter<I: iter::IntoIterator<Item = char>>(iter: I) -> SmolStr {
let iter = iter.into_iter();
Self::from_char_iter(iter)
}
}
fn build_from_str_iter<T>(mut iter: impl Iterator<Item = T>) -> SmolStr
where
T: AsRef<str>,
String: iter::Extend<T>,
{
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()));
}
buf[len..][..size].copy_from_slice(slice.as_bytes());
len += size;
}
SmolStr(Repr::Inline {
// SAFETY: We know that `len` is less than or equal to the maximum value of `InlineSize`
// as we otherwise return early.
len: unsafe { InlineSize::transmute_from_u8(len as u8) },
buf,
})
}
impl iter::FromIterator<String> for SmolStr {
fn from_iter<I: iter::IntoIterator<Item = String>>(iter: I) -> SmolStr {
build_from_str_iter(iter.into_iter())
}
}
impl<'a> iter::FromIterator<&'a String> for SmolStr {
fn from_iter<I: iter::IntoIterator<Item = &'a String>>(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<I: iter::IntoIterator<Item = &'a str>>(iter: I) -> SmolStr {
build_from_str_iter(iter.into_iter())
}
}
impl AsRef<str> 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<String> for SmolStr {
#[inline(always)]
fn from(text: String) -> Self {
Self::new(text)
}
}
impl From<Box<str>> for SmolStr {
#[inline]
fn from(s: Box<str>) -> SmolStr {
SmolStr::new(s)
}
}
impl From<Arc<str>> for SmolStr {
#[inline]
fn from(s: Arc<str>) -> SmolStr {
let repr = Repr::new_on_stack(s.as_ref()).unwrap_or_else(|| Repr::Heap(s));
Self(repr)
}
}
impl<'a> From<Cow<'a, str>> for SmolStr {
#[inline]
fn from(s: Cow<'a, str>) -> SmolStr {
SmolStr::new(s)
}
}
impl From<SmolStr> for Arc<str> {
#[inline(always)]
fn from(text: SmolStr) -> Self {
match text.0 {
Repr::Heap(data) => data,
_ => text.as_str().into(),
}
}
}
impl From<SmolStr> for String {
#[inline(always)]
fn from(text: SmolStr) -> Self {
text.as_str().into()
}
}
impl Borrow<str> 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<SmolStr, Self::Err> {
Ok(SmolStr::from(s))
}
}
#[cfg(feature = "arbitrary")]
impl<'a> arbitrary::Arbitrary<'a> for SmolStr {
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> Result<Self, arbitrary::Error> {
let s = <&str>::arbitrary(u)?;
Ok(SmolStr::new(s))
}
}
const INLINE_CAP: usize = InlineSize::_V23 as usize;
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 ";
const _: () = {
assert!(WS.len() == N_NEWLINES + N_SPACES);
assert!(WS.as_bytes()[N_NEWLINES - 1] == b'\n');
assert!(WS.as_bytes()[N_NEWLINES] == b' ');
};
#[derive(Clone, Copy, Debug, PartialEq)]
#[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,
}
impl InlineSize {
#[inline(always)]
const unsafe fn transmute_from_u8(value: u8) -> Self {
debug_assert!(value <= InlineSize::_V23 as u8);
unsafe { transmute::<u8, Self>(value) }
}
}
#[derive(Clone, Debug)]
enum Repr {
Inline {
len: InlineSize,
buf: [u8; INLINE_CAP],
},
Static(&'static str),
Heap(Arc<str>),
}
impl Repr {
/// This function tries to create a new Repr::Inline or Repr::Static
/// If it isn't possible, this function returns None
fn new_on_stack<T>(text: T) -> Option<Self>
where
T: AsRef<str>,
{
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 Some(Repr::Inline {
// SAFETY: We know that `len` is less than or equal to the maximum value of `InlineSize`
len: unsafe { InlineSize::transmute_from_u8(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;
let substring = &WS[N_NEWLINES - newlines..N_NEWLINES + spaces];
return Some(Repr::Static(substring));
}
}
None
}
fn new<T>(text: T) -> Self
where
T: AsRef<str>,
{
Self::new_on_stack(text.as_ref()).unwrap_or_else(|| Repr::Heap(text.as_ref().into()))
}
#[inline(always)]
fn len(&self) -> usize {
match self {
Repr::Heap(data) => data.len(),
Repr::Static(data) => data.len(),
Repr::Inline { len, .. } => *len as usize,
}
}
#[inline(always)]
fn is_empty(&self) -> bool {
match self {
Repr::Heap(data) => data.is_empty(),
Repr::Static(data) => data.is_empty(),
Repr::Inline { len, .. } => *len as u8 == 0,
}
}
#[inline]
fn as_str(&self) -> &str {
match self {
Repr::Heap(data) => data,
Repr::Static(data) => data,
Repr::Inline { len, buf } => {
let len = *len as usize;
let buf = &buf[..len];
// SAFETY: buf is guaranteed to be valid utf8 for ..len bytes
unsafe { ::core::str::from_utf8_unchecked(buf) }
}
}
}
fn ptr_eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::Heap(l0), Self::Heap(r0)) => Arc::ptr_eq(l0, r0),
(Self::Static(l0), Self::Static(r0)) => core::ptr::eq(l0, r0),
(
Self::Inline {
len: l_len,
buf: l_buf,
},
Self::Inline {
len: r_len,
buf: r_buf,
},
) => l_len == r_len && l_buf == r_buf,
_ => false,
}
}
}
/// 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;
}
/// [`str`] methods producing [`SmolStr`]s.
pub trait StrExt: private::Sealed {
/// Returns the lowercase equivalent of this string slice as a new [`SmolStr`],
/// potentially without allocating.
///
/// See [`str::to_lowercase`].
#[must_use = "this returns a new SmolStr without modifying the original"]
fn to_lowercase_smolstr(&self) -> SmolStr;
/// Returns the uppercase equivalent of this string slice as a new [`SmolStr`],
/// potentially without allocating.
///
/// See [`str::to_uppercase`].
#[must_use = "this returns a new SmolStr without modifying the original"]
fn to_uppercase_smolstr(&self) -> SmolStr;
/// Returns the ASCII lowercase equivalent of this string slice as a new [`SmolStr`],
/// potentially without allocating.
///
/// See [`str::to_ascii_lowercase`].
#[must_use = "this returns a new SmolStr without modifying the original"]
fn to_ascii_lowercase_smolstr(&self) -> SmolStr;
/// Returns the ASCII uppercase equivalent of this string slice as a new [`SmolStr`],
/// potentially without allocating.
///
/// See [`str::to_ascii_uppercase`].
#[must_use = "this returns a new SmolStr without modifying the original"]
fn to_ascii_uppercase_smolstr(&self) -> SmolStr;
/// Replaces all matches of a &str with another &str returning a new [`SmolStr`],
/// potentially without allocating.
///
/// See [`str::replace`].
// TODO: Use `Pattern` when stable.
#[must_use = "this returns a new SmolStr without modifying the original"]
fn replace_smolstr(&self, from: &str, to: &str) -> SmolStr;
/// Replaces first N matches of a &str with another &str returning a new [`SmolStr`],
/// potentially without allocating.
///
/// See [`str::replacen`].
// TODO: Use `Pattern` when stable.
#[must_use = "this returns a new SmolStr without modifying the original"]
fn replacen_smolstr(&self, from: &str, to: &str, count: usize) -> SmolStr;
}
impl StrExt for str {
#[inline]
fn to_lowercase_smolstr(&self) -> SmolStr {
SmolStr::from_char_iter(self.chars().flat_map(|c| c.to_lowercase()))
}
#[inline]
fn to_uppercase_smolstr(&self) -> SmolStr {
SmolStr::from_char_iter(self.chars().flat_map(|c| c.to_uppercase()))
}
#[inline]
fn to_ascii_lowercase_smolstr(&self) -> SmolStr {
SmolStr::from_char_iter(self.chars().map(|c| c.to_ascii_lowercase()))
}
#[inline]
fn to_ascii_uppercase_smolstr(&self) -> SmolStr {
SmolStr::from_char_iter(self.chars().map(|c| c.to_ascii_uppercase()))
}
#[inline]
fn replace_smolstr(&self, from: &str, to: &str) -> SmolStr {
self.replacen_smolstr(from, to, usize::MAX)
}
#[inline]
fn replacen_smolstr(&self, from: &str, to: &str, count: usize) -> SmolStr {
let mut result = Writer::new();
let mut last_end = 0;
for (start, part) in self.match_indices(from).take(count) {
// SAFETY: `start` is guaranteed to be within the bounds of `self` as per
// `match_indices` and last_end is always less than or equal to `start`
result.push_str(unsafe { self.get_unchecked(last_end..start) });
result.push_str(to);
last_end = start + part.len();
}
// SAFETY: `self.len()` is guaranteed to be within the bounds of `self` and last_end is
// always less than or equal to `self.len()`
result.push_str(unsafe { self.get_unchecked(last_end..self.len()) });
SmolStr::from(result)
}
}
mod private {
/// No downstream impls allowed.
pub trait Sealed {}
impl Sealed for str {}
}
/// 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)*) => {{
use ::core::fmt::Write;
let mut w = $crate::Writer::new();
w.write_fmt(format_args!($($tt)*)).expect("a formatting trait implementation returned an error");
$crate::SmolStr::from(w)
}};
}
#[doc(hidden)]
pub struct Writer {
inline: [u8; INLINE_CAP],
heap: String,
len: usize,
}
impl Writer {
#[must_use]
pub const fn new() -> Self {
Writer {
inline: [0; INLINE_CAP],
heap: String::new(),
len: 0,
}
}
fn push_str(&mut self, s: &str) {
// if currently on the stack
if self.len <= INLINE_CAP {
let old_len = self.len;
self.len += s.len();
// if the new length will fit on the stack (even if it fills it entirely)
if self.len <= INLINE_CAP {
self.inline[old_len..self.len].copy_from_slice(s.as_bytes());
return; // skip the heap push below
}
self.heap.reserve(self.len);
// copy existing inline bytes over to the heap
// SAFETY: inline data is guaranteed to be valid utf8 for `old_len` bytes
unsafe {
self.heap
.as_mut_vec()
.extend_from_slice(&self.inline[..old_len]);
}
}
self.heap.push_str(s);
}
}
impl fmt::Write for Writer {
#[inline]
fn write_str(&mut self, s: &str) -> fmt::Result {
self.push_str(s);
Ok(())
}
}
impl From<Writer> for SmolStr {
fn from(value: Writer) -> Self {
SmolStr(if value.len <= INLINE_CAP {
Repr::Inline {
// SAFETY: We know that `value.len` is less than or equal to the maximum value of `InlineSize`
len: unsafe { InlineSize::transmute_from_u8(value.len as u8) },
buf: value.inline,
}
} else {
Repr::new(value.heap)
})
}
}
impl<T> ToSmolStr for T
where
T: fmt::Display + ?Sized,
{
fn to_smolstr(&self) -> SmolStr {
format_smolstr!("{}", self)
}
}
#[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<SmolStr, D::Error>
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<E>(self, v: &str) -> Result<Self::Value, E>
where
E: Error,
{
Ok(SmolStr::from(v))
}
fn visit_borrowed_str<E>(self, v: &'a str) -> Result<Self::Value, E>
where
E: Error,
{
Ok(SmolStr::from(v))
}
fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
where
E: Error,
{
Ok(SmolStr::from(v))
}
fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
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<E>(self, v: &'a [u8]) -> Result<Self::Value, E>
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<E>(self, v: Vec<u8>) -> Result<Self::Value, E>
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<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
self.as_str().serialize(serializer)
}
}
impl<'de> serde::Deserialize<'de> for SmolStr {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
smol_str(deserializer)
}
}
}

332
vendor/smol_str/tests/test.rs vendored Normal file
View File

@@ -0,0 +1,332 @@
use std::sync::Arc;
#[cfg(not(miri))]
use 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::<SmolStr>(),
::std::mem::size_of::<String>(),
);
}
#[test]
fn assert_traits() {
fn f<T: Send + Sync + ::std::fmt::Debug + Clone>() {}
f::<SmolStr>();
}
#[test]
fn conversions() {
let s: SmolStr = "Hello, World!".into();
let s: String = s.into();
assert_eq!(s, "Hello, World!");
let s: SmolStr = Arc::<str>::from("Hello, World!").into();
let s: Arc<str> = s.into();
assert_eq!(s.as_ref(), "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"));
}
#[cfg(not(miri))]
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(())
}
#[cfg(not(miri))]
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<SmolStr>,
pub(crate) map: HashMap<SmolStr, SmolStr>,
}
#[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<SmolStr, SmolStr> = 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<SmolStr, SmolStr> =
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<SmolStr> = 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<SmolStr> = serde_json::from_reader(std::io::Cursor::new(s)).unwrap();
}
}
#[test]
fn test_search_in_hashmap() {
let mut m = ::std::collections::HashMap::<SmolStr, i32>::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鷗Œé逍Üߪąñ丂㐀𠀀", 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 = "a".repeat(23 * 4 + 1);
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>(I);
impl<T, I: Iterator<Item = T>> Iterator for BadSizeHint<I> {
type Item = T;
fn next(&mut self) -> Option<Self::Item> {
self.0.next()
}
fn size_hint(&self) -> (usize, Option<usize>) {
(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);
}
#[test]
fn test_to_smolstr() {
use smol_str::ToSmolStr;
for i in 0..26 {
let a = &"abcdefghijklmnopqrstuvwxyz"[i..];
assert_eq!(a, a.to_smolstr());
assert_eq!(a, smol_str::format_smolstr!("{}", a));
}
}
#[cfg(test)]
mod test_str_ext {
use smol_str::StrExt;
#[test]
fn large() {
let lowercase = "aaaaaaAAAAAaaaaaaaaaaaaaaaaaaaaaAAAAaaaaaaaaaaaaaa".to_lowercase_smolstr();
assert_eq!(
lowercase,
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
);
assert!(lowercase.is_heap_allocated());
}
#[test]
fn to_lowercase() {
let lowercase = "aßΔC".to_lowercase_smolstr();
assert_eq!(lowercase, "aßδc");
assert!(!lowercase.is_heap_allocated());
}
#[test]
fn to_uppercase() {
let uppercase = "aßΔC".to_uppercase_smolstr();
assert_eq!(uppercase, "ASSΔC");
assert!(!uppercase.is_heap_allocated());
}
#[test]
fn to_ascii_lowercase() {
let uppercase = "aßΔC".to_ascii_lowercase_smolstr();
assert_eq!(uppercase, "aßΔc");
assert!(!uppercase.is_heap_allocated());
}
#[test]
fn to_ascii_uppercase() {
let uppercase = "aßΔC".to_ascii_uppercase_smolstr();
assert_eq!(uppercase, "AßΔC");
assert!(!uppercase.is_heap_allocated());
}
#[test]
fn replace() {
let result = "foo_bar_baz".replace_smolstr("ba", "do");
assert_eq!(result, "foo_dor_doz");
assert!(!result.is_heap_allocated());
}
#[test]
fn replacen() {
let result = "foo_bar_baz".replacen_smolstr("ba", "do", 1);
assert_eq!(result, "foo_dor_baz");
assert!(!result.is_heap_allocated());
}
}

47
vendor/smol_str/tests/tidy.rs vendored Normal file
View File

@@ -0,0 +1,47 @@
#![cfg(not(miri))]
use 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<Path>) -> 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`")
}
}