Skip to content

Commit

Permalink
Add a shared uv-small-str crate (#10545)
Browse files Browse the repository at this point in the history
## Summary

I want to use `SmallString` elsewhere.
  • Loading branch information
charliermarsh authored Jan 12, 2025
1 parent 4ca5e04 commit 1e48c12
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 13 deletions.
12 changes: 11 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,11 @@ uv-resolver = { path = "crates/uv-resolver" }
uv-scripts = { path = "crates/uv-scripts" }
uv-settings = { path = "crates/uv-settings" }
uv-shell = { path = "crates/uv-shell" }
uv-small-str = { path = "crates/uv-small-str" }
uv-state = { path = "crates/uv-state" }
uv-static = { path = "crates/uv-static" }
uv-trampoline-builder = { path = "crates/uv-trampoline-builder" }
uv-tool = { path = "crates/uv-tool" }
uv-trampoline-builder = { path = "crates/uv-trampoline-builder" }
uv-types = { path = "crates/uv-types" }
uv-version = { path = "crates/uv-version" }
uv-virtualenv = { path = "crates/uv-virtualenv" }
Expand Down
6 changes: 5 additions & 1 deletion crates/uv-normalize/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ doctest = false
workspace = true

[dependencies]
arcstr = { workspace = true }
uv-small-str = { workspace = true }

rkyv = { workspace = true }
schemars = { workspace = true, optional = true }
serde = { workspace = true, features = ["derive"] }

[features]
schemars = ["dep:schemars", "uv-small-str/schemars"]
3 changes: 2 additions & 1 deletion crates/uv-normalize/src/extra_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use std::str::FromStr;

use serde::{Deserialize, Deserializer, Serialize};

use crate::small_string::SmallString;
use uv_small_str::SmallString;

use crate::{validate_and_normalize_owned, validate_and_normalize_ref, InvalidNameError};

/// The normalized name of an extra dependency.
Expand Down
3 changes: 2 additions & 1 deletion crates/uv-normalize/src/group_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use std::sync::LazyLock;

use serde::{Deserialize, Deserializer, Serialize, Serializer};

use crate::small_string::SmallString;
use uv_small_str::SmallString;

use crate::{validate_and_normalize_owned, validate_and_normalize_ref, InvalidNameError};

/// The normalized name of a dependency group.
Expand Down
4 changes: 2 additions & 2 deletions crates/uv-normalize/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ pub use dist_info_name::DistInfoName;
pub use extra_name::ExtraName;
pub use group_name::{GroupName, DEV_DEPENDENCIES};
pub use package_name::PackageName;
use small_string::SmallString;

use uv_small_str::SmallString;

mod dist_info_name;
mod extra_name;
mod group_name;
mod package_name;
mod small_string;

/// Validate and normalize an owned package or extra name.
pub(crate) fn validate_and_normalize_owned(name: String) -> Result<SmallString, InvalidNameError> {
Expand Down
3 changes: 2 additions & 1 deletion crates/uv-normalize/src/package_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use std::str::FromStr;

use serde::{Deserialize, Deserializer, Serialize};

use crate::small_string::SmallString;
use uv_small_str::SmallString;

use crate::{validate_and_normalize_owned, validate_and_normalize_ref, InvalidNameError};

/// The normalized name of a package.
Expand Down
22 changes: 22 additions & 0 deletions crates/uv-small-str/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[package]
name = "uv-small-str"
version = "0.0.1"
edition = { workspace = true }
rust-version = { workspace = true }
homepage = { workspace = true }
documentation = { workspace = true }
repository = { workspace = true }
authors = { workspace = true }
license = { workspace = true }

[lib]
doctest = false

[lints]
workspace = true

[dependencies]
arcstr = { workspace = true }
rkyv = { workspace = true }
schemars = { workspace = true, optional = true }
serde = { workspace = true }
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
use std::cmp::PartialEq;
use std::ops::Deref;

/// An optimized small string type for short identifiers, like package names.
///
/// Represented as an [`arcstr::ArcStr`] internally.
#[derive(Default, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub(crate) struct SmallString(arcstr::ArcStr);
/// An optimized type for immutable identifiers. Represented as an [`arcstr::ArcStr`] internally.
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct SmallString(arcstr::ArcStr);

impl From<arcstr::ArcStr> for SmallString {
#[inline]
fn from(s: arcstr::ArcStr) -> Self {
Self(s)
}
}

impl From<&str> for SmallString {
#[inline]
Expand All @@ -28,6 +33,13 @@ impl AsRef<str> for SmallString {
}
}

impl core::borrow::Borrow<str> for SmallString {
#[inline]
fn borrow(&self) -> &str {
self
}
}

impl Deref for SmallString {
type Target = str;

Expand Down

0 comments on commit 1e48c12

Please sign in to comment.