summaryrefslogtreecommitdiffstats
path: root/third_party/rust/strck
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
commit26a029d407be480d791972afb5975cf62c9360a6 (patch)
treef435a8308119effd964b339f76abb83a57c29483 /third_party/rust/strck
parentInitial commit. (diff)
downloadfirefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz
firefox-26a029d407be480d791972afb5975cf62c9360a6.zip
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/strck')
-rw-r--r--third_party/rust/strck/.cargo-checksum.json1
-rw-r--r--third_party/rust/strck/Cargo.toml47
-rw-r--r--third_party/rust/strck/LICENSE8
-rw-r--r--third_party/rust/strck/README.md103
-rw-r--r--third_party/rust/strck/src/lib.rs508
-rw-r--r--third_party/rust/strck/src/partial_eq.rs94
-rw-r--r--third_party/rust/strck/src/serde.rs78
7 files changed, 839 insertions, 0 deletions
diff --git a/third_party/rust/strck/.cargo-checksum.json b/third_party/rust/strck/.cargo-checksum.json
new file mode 100644
index 0000000000..95f631e4a2
--- /dev/null
+++ b/third_party/rust/strck/.cargo-checksum.json
@@ -0,0 +1 @@
+{"files":{"Cargo.toml":"2ca9b5322a0cebfbefd1e25b324c26285ecc7865233c2f1734d7f8d49730a7e9","LICENSE":"7e24648e3d082d4f8026cdedb21d084d91ef13223bff6b76cb8bf19a744b7d28","README.md":"4de456ce24af35b5a1a54c68065971112f0a6123738476387b81030879eddbfb","src/lib.rs":"311e7e0be89d597198530a50e081d28330ee4335f387267e360a3bf117185329","src/partial_eq.rs":"791a0a2ae45b87a63cfb424bc67c30d661db56b31953efc4889de86c44bf483d","src/serde.rs":"cbbc441e28212f61920bee49a29d188dda869e2ee7fae98dfab096d386e418aa"},"package":"be91090ded9d8f979d9fe921777342d37e769e0b6b7296843a7a38247240e917"} \ No newline at end of file
diff --git a/third_party/rust/strck/Cargo.toml b/third_party/rust/strck/Cargo.toml
new file mode 100644
index 0000000000..ae4788b7d5
--- /dev/null
+++ b/third_party/rust/strck/Cargo.toml
@@ -0,0 +1,47 @@
+# 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 = "2021"
+name = "strck"
+version = "0.1.2"
+authors = ["Quinn Okabayashi <quinnokabayashi@gmail.com>"]
+description = "Checked owned and borrowed strings"
+readme = "README.md"
+keywords = [
+ "text",
+ "ident",
+ "identifier",
+ "validate",
+]
+categories = [
+ "rust-patterns",
+ "memory-management",
+]
+license = "MIT"
+repository = "https://github.com/QnnOkabayashi/strck"
+
+[package.metadata.docs.rs]
+all-features = true
+
+[dependencies.serde]
+version = "1.0"
+optional = true
+
+[dev-dependencies.serde]
+version = "1.0"
+features = ["derive"]
+
+[dev-dependencies.serde_json]
+version = "1.0"
+
+[dev-dependencies.smol_str]
+version = "0.1"
diff --git a/third_party/rust/strck/LICENSE b/third_party/rust/strck/LICENSE
new file mode 100644
index 0000000000..3752b79341
--- /dev/null
+++ b/third_party/rust/strck/LICENSE
@@ -0,0 +1,8 @@
+Copyright 2022 Quinn Okabayashi
+
+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.
+
diff --git a/third_party/rust/strck/README.md b/third_party/rust/strck/README.md
new file mode 100644
index 0000000000..02471df01c
--- /dev/null
+++ b/third_party/rust/strck/README.md
@@ -0,0 +1,103 @@
+[![github-img]][github-url] [![crates-img]][crates-url] [![docs-img]][docs-url]
+
+Checked owned and borrowed strings.
+
+# Overview
+
+The Rust standard library provides the `String` and `str` types, which wrap
+`Vec<u8>` and `[u8]` respectively, with the invariant that the contents
+are valid UTF-8.
+
+This crate abstracts the idea of type-level invariants on strings by
+introducing the immutable `Check` and `Ck` types, where the invariants
+are determined by a generic `Invariant` type parameter. Implementing
+the `Invariant` trait is left to other crates, such as [`strck_ident`].
+
+"strck" comes from "str check", similar to how rustc has typeck and
+borrowck for type check and borrow check respectively.
+
+See [the documentation][docs-url] for more details.
+
+# Motivation
+
+Libraries working with string-like types with certain properties, like identifiers,
+quickly become confusing as `&str` and `String` begin to pollute type signatures
+everywhere. One solution is to manually implement an owned checked string type
+like `syn::Ident` to disambiguate the type signatures and validate the string.
+The downside is that new values cannot be created without allocation,
+which is unnecessary when only a borrowed version is required.
+
+`strck` solves this issue by providing a checked borrowed string type, `Ck`,
+alongside a checked owned string type, `Check`. These serve as thin wrappers
+around `str` and `String`[^1] respectively, and prove at the type level that
+the contents satisfy the `Invariant` that the wrapper is generic over.
+
+[^1]: `Check` can actually be backed by any `'static + AsRef<str>` type,
+but `String` is the default.
+
+# Use cases
+
+### Checked strings without allocating
+
+The main benefit `strck` offers is validating borrowed strings via the
+`Ck` type without having to allocate in the result.
+
+```rust
+use strck_ident::{Ck, IntoCk, rust::RustIdent};
+
+let this_ident: &Ck<RustIdent> = "this".ck().unwrap();
+```
+
+### Checked zero-copy deserialization
+
+When the `serde` feature flag is enabled, `Ck`s can be used to perform
+checked zero-copy deserialization, which requires the
+`#[serde(borrow)]` attribute.
+
+```rust
+use strck_ident::{Ck, unicode::UnicodeIdent};
+
+#[derive(Serialize, Deserialize)]
+struct Player<'a> {
+ #[serde(borrow)]
+ username: &'a Ck<UnicodeIdent>,
+ level: u32,
+}
+```
+
+Note that this code sample explicitly uses `Ck<UnicodeIdent>` to demonstrate
+that the type is a `Ck`. However, `strck_ident` provides `Ident` as an
+alias for `Ck<UnicodeIdent>`, which should be used in practice.
+
+
+### Infallible parsing
+
+For types where string validation is relatively cheap but parsing is costly
+and fallible, `strck` can be used with a custom `Invariant` as an input to
+make an infallible parsing function.
+
+# Postfix construction with `IntoCk` and `IntoCheck`
+
+This crate exposes two helper traits, `IntoCk` and `IntoCheck`. When in
+scope, the `.ck()` and `.check()` functions can be used to create
+`Ck`s and `Check`s respectively:
+
+```rust
+use strck_ident::{IntoCheck, IntoCk, unicode::UnicodeIdent};
+
+let this_ident = "this".ck::<UnicodeIdent>().unwrap();
+let this_foo_ident = format!("{}_foo", this_ident).check::<UnicodeIdent>().unwrap();
+```
+
+# Documentation
+
+See the [crate-level documentation][docs-url] for more details.
+
+[`strck_ident`]: https://docs.rs/strck_ident
+
+[github-url]: https://github.com/QnnOkabayashi/strck
+[crates-url]: https://crates.io/crates/strck
+[docs-url]: https://docs.rs/strck
+[github-img]: https://img.shields.io/badge/github-8da0cb?style=for-the-badge&labelColor=555555&logo=github
+[crates-img]: https://img.shields.io/badge/crates.io-fc8d62?style=for-the-badge&labelColor=555555&logo=rust
+[docs-img]: https://img.shields.io/badge/docs.rs-66c2a5?style=for-the-badge&labelColor=555555&logoColor=white&logo=data:image/svg+xml;base64,PHN2ZyByb2xlPSJpbWciIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmlld0JveD0iMCAwIDUxMiA1MTIiPjxwYXRoIGZpbGw9IiNmNWY1ZjUiIGQ9Ik00ODguNiAyNTAuMkwzOTIgMjE0VjEwNS41YzAtMTUtOS4zLTI4LjQtMjMuNC0zMy43bC0xMDAtMzcuNWMtOC4xLTMuMS0xNy4xLTMuMS0yNS4zIDBsLTEwMCAzNy41Yy0xNC4xIDUuMy0yMy40IDE4LjctMjMuNCAzMy43VjIxNGwtOTYuNiAzNi4yQzkuMyAyNTUuNSAwIDI2OC45IDAgMjgzLjlWMzk0YzAgMTMuNiA3LjcgMjYuMSAxOS45IDMyLjJsMTAwIDUwYzEwLjEgNS4xIDIyLjEgNS4xIDMyLjIgMGwxMDMuOS01MiAxMDMuOSA1MmMxMC4xIDUuMSAyMi4xIDUuMSAzMi4yIDBsMTAwLTUwYzEyLjItNi4xIDE5LjktMTguNiAxOS45LTMyLjJWMjgzLjljMC0xNS05LjMtMjguNC0yMy40LTMzLjd6TTM1OCAyMTQuOGwtODUgMzEuOXYtNjguMmw4NS0zN3Y3My4zek0xNTQgMTA0LjFsMTAyLTM4LjIgMTAyIDM4LjJ2LjZsLTEwMiA0MS40LTEwMi00MS40di0uNnptODQgMjkxLjFsLTg1IDQyLjV2LTc5LjFsODUtMzguOHY3NS40em0wLTExMmwtMTAyIDQxLjQtMTAyLTQxLjR2LS42bDEwMi0zOC4yIDEwMiAzOC4ydi42em0yNDAgMTEybC04NSA0Mi41di03OS4xbDg1LTM4Ljh2NzUuNHptMC0xMTJsLTEwMiA0MS40LTEwMi00MS40di0uNmwxMDItMzguMiAxMDIgMzguMnYuNnoiPjwvcGF0aD48L3N2Zz4K
diff --git a/third_party/rust/strck/src/lib.rs b/third_party/rust/strck/src/lib.rs
new file mode 100644
index 0000000000..9bd3b5d287
--- /dev/null
+++ b/third_party/rust/strck/src/lib.rs
@@ -0,0 +1,508 @@
+//! [![github-img]][github-url] [![crates-img]][crates-url] [![docs-img]][docs-url]
+//!
+//! [github-url]: https://github.com/QnnOkabayashi/strck
+//! [crates-url]: https://crates.io/crates/strck
+//! [docs-url]: crate
+//! [github-img]: https://img.shields.io/badge/github-8da0cb?style=for-the-badge&labelColor=555555&logo=github
+//! [crates-img]: https://img.shields.io/badge/crates.io-fc8d62?style=for-the-badge&labelColor=555555&logo=rust
+//! [docs-img]: https://img.shields.io/badge/docs.rs-66c2a5?style=for-the-badge&labelColor=555555&logoColor=white&logo=data:image/svg+xml;base64,PHN2ZyByb2xlPSJpbWciIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmlld0JveD0iMCAwIDUxMiA1MTIiPjxwYXRoIGZpbGw9IiNmNWY1ZjUiIGQ9Ik00ODguNiAyNTAuMkwzOTIgMjE0VjEwNS41YzAtMTUtOS4zLTI4LjQtMjMuNC0zMy43bC0xMDAtMzcuNWMtOC4xLTMuMS0xNy4xLTMuMS0yNS4zIDBsLTEwMCAzNy41Yy0xNC4xIDUuMy0yMy40IDE4LjctMjMuNCAzMy43VjIxNGwtOTYuNiAzNi4yQzkuMyAyNTUuNSAwIDI2OC45IDAgMjgzLjlWMzk0YzAgMTMuNiA3LjcgMjYuMSAxOS45IDMyLjJsMTAwIDUwYzEwLjEgNS4xIDIyLjEgNS4xIDMyLjIgMGwxMDMuOS01MiAxMDMuOSA1MmMxMC4xIDUuMSAyMi4xIDUuMSAzMi4yIDBsMTAwLTUwYzEyLjItNi4xIDE5LjktMTguNiAxOS45LTMyLjJWMjgzLjljMC0xNS05LjMtMjguNC0yMy40LTMzLjd6TTM1OCAyMTQuOGwtODUgMzEuOXYtNjguMmw4NS0zN3Y3My4zek0xNTQgMTA0LjFsMTAyLTM4LjIgMTAyIDM4LjJ2LjZsLTEwMiA0MS40LTEwMi00MS40di0uNnptODQgMjkxLjFsLTg1IDQyLjV2LTc5LjFsODUtMzguOHY3NS40em0wLTExMmwtMTAyIDQxLjQtMTAyLTQxLjR2LS42bDEwMi0zOC4yIDEwMiAzOC4ydi42em0yNDAgMTEybC04NSA0Mi41di03OS4xbDg1LTM4Ljh2NzUuNHptMC0xMTJsLTEwMiA0MS40LTEwMi00MS40di0uNmwxMDItMzguMiAxMDIgMzguMnYuNnoiPjwvcGF0aD48L3N2Zz4K
+//!
+//! Checked owned and borrowed strings.
+//!
+//! # Overview
+//!
+//! The Rust standard library provides the `String` and `str` types, which wrap
+//! `Vec<u8>` and `[u8]` respectively, with the invariant that the contents
+//! are valid UTF-8.
+//!
+//! This crate abstracts the idea of type-level invariants on strings by
+//! introducing the immutable [`Check`] and [`Ck`] types, where the invariants
+//! are determined by a generic [`Invariant`] type parameter. Implementing
+//! the [`Invariant`] trait is left to other crates, such as [`strck_ident`].
+//!
+//! "strck" comes from "str check", similar to how rustc has typeck and
+//! borrowck for type check and borrow check respectively.
+//!
+//! # Motivation
+//!
+//! Libraries working with string-like types with certain properties, like identifiers,
+//! quickly become confusing as `&str` and `String` begin to pollute type signatures
+//! everywhere. One solution is to manually implement an owned checked string type
+//! like [`syn::Ident`] to disambiguate the type signatures and validate the string.
+//! The downside is that new values cannot be created without allocation,
+//! which is unnecessary when only a borrowed version is required.
+//!
+//! `strck` solves this issue by providing a checked borrowed string type, [`Ck`],
+//! alongside a checked owned string type, [`Check`]. These serve as thin wrappers
+//! around `str` and `String`[^1] respectively, and prove at the type level that
+//! the contents satisfy the [`Invariant`] that the wrapper is generic over.
+//!
+//! [^1]: [`Check`] can actually be backed by any `'static + AsRef<str>` type,
+//! but `String` is the default.
+//!
+//! # Use cases
+//!
+//! ### Checked strings without allocating
+//!
+//! The main benefit `strck` offers is validating borrowed strings via the
+//! [`Ck`] type without having to allocate in the result.
+//!
+//! ```rust
+//! use strck_ident::{Ck, IntoCk, rust::RustIdent};
+//!
+//! let this_ident: &Ck<RustIdent> = "this".ck().unwrap();
+//! ```
+//!
+//! ### Checked zero-copy deserialization
+//!
+//! When the `serde` feature flag is enabled, [`Ck`]s can be used to perform
+//! checked zero-copy deserialization, which requires the
+//! [`#[serde(borrow)]`][borrow] attribute.
+//!
+//! ```rust
+//! # use serde::{Serialize, Deserialize};
+//! use strck_ident::{Ck, unicode::UnicodeIdent};
+//!
+//! #[derive(Serialize, Deserialize)]
+//! struct Player<'a> {
+//! #[serde(borrow)]
+//! username: &'a Ck<UnicodeIdent>,
+//! level: u32,
+//! }
+//! ```
+//!
+//! Note that this code sample explicitly uses `Ck<UnicodeIdent>` to demonstrate
+//! that the type is a [`Ck`]. However, [`strck_ident`] provides [`Ident`] as an
+//! alias for `Ck<UnicodeIdent>`, which should be used in practice.
+//!
+//! ### Infallible parsing
+//!
+//! For types where string validation is relatively cheap but parsing is costly
+//! and fallible, `strck` can be used with a custom [`Invariant`] as an input to
+//! make an infallible parsing function.
+//!
+//! # Postfix construction with `IntoCk` and `IntoCheck`
+//!
+//! This crate exposes two helper traits, [`IntoCk`] and [`IntoCheck`]. When in
+//! scope, the [`.ck()`] and [`.check()`] functions can be used to create
+//! [`Ck`]s and [`Check`]s respectively:
+//!
+//! ```rust
+//! use strck_ident::{IntoCheck, IntoCk, unicode::UnicodeIdent};
+//!
+//! let this_ident = "this".ck::<UnicodeIdent>().unwrap();
+//! let this_foo_ident = format!("{}_foo", this_ident).check::<UnicodeIdent>().unwrap();
+//! ```
+//!
+//! # Feature flags
+//!
+//! * `serde`: Implements `Serialize`/`Deserialize` for [`Check`]s and [`Ck`]s,
+//! where the invariants are checked during deserialization. Disabled by default.
+//!
+//! [`syn::Ident`]: https://docs.rs/syn/latest/syn/struct.Ident.html
+//! [`strck_ident`]: https://docs.rs/strck_ident
+//! [`Ident`]: https://docs.rs/strck_ident/latest/strck_ident/unicode/type.Ident.html
+//! [borrow]: https://serde.rs/lifetimes.html#borrowing-data-in-a-derived-impl
+//! [`.ck()`]: IntoCk::ck
+//! [`.check()`]: IntoCheck::check
+use core::{borrow, cmp, fmt, hash, marker, ops, str};
+
+mod partial_eq;
+#[cfg(feature = "serde")]
+mod serde;
+
+/// Owned immutable string with invariants.
+///
+/// Similar to how `String` derefs to `&str`, [`Check`] derefs to [`&Ck`](Ck).
+/// This means APIs requiring `&Check<I>` as an argument should instead consider
+/// accepting `&Ck<I>` for more flexibility.
+///
+/// # Buffers
+///
+/// By default, this type is backed by a `String`, but it can also be backed by
+/// any `AsRef<str> + 'static` type. In particular, types like [`SmolStr`] are
+/// good candidates since they're designed to be immutable.
+///
+/// It's recommended to use a type alias when using a custom backing type, since
+/// extra generics can make the type signature long.
+///
+/// [`SmolStr`]: https://docs.rs/smol_str/latest/smol_str/struct.SmolStr.html
+#[derive(Clone)]
+#[repr(transparent)]
+pub struct Check<I: Invariant, B: AsRef<str> + 'static = String> {
+ _marker: marker::PhantomData<I>,
+ buf: B,
+}
+
+/// Borrowed immutable string with invariants.
+///
+/// [`Ck`] is a DST, and therefore must always live behind a pointer. This means
+/// you'll usually see it as `&Ck<I>` in type signatures.
+///
+/// # Deserialization
+///
+/// See the [crate-level documentation] for details on how to use [`Ck`] for
+/// checked zero-copy deserialization.
+///
+/// [crate-level documentation]: crate#checked-zero-copy-deserialization
+#[repr(transparent)]
+pub struct Ck<I: Invariant> {
+ _marker: marker::PhantomData<I>,
+ slice: str,
+}
+
+/// Invariant for a [`Ck`] or [`Check`].
+///
+/// The [`Ck`] and [`Check`] types are checked strings types that make guarantees
+/// about the contents of the string. These guarantees are determined by this
+/// trait, `Invariant` which distinguishes whether or not a string upholds some
+/// arbitrary invariants via the [`Invariant::check`] function. If the `Err` is
+/// returned, then the invariant is broken, and the `Ck` or `Check` generic over
+/// the invariant cannot be constructed.
+///
+/// # Examples
+///
+/// Declaring an invariant that the string contains no whitespace:
+/// ```rust
+/// # use strck::Invariant;
+/// struct NoWhitespace;
+///
+/// impl Invariant for NoWhitespace {
+/// type Error = char;
+///
+/// fn check(slice: &str) -> Result<(), Self::Error> {
+/// match slice.chars().find(|ch| ch.is_whitespace()) {
+/// Some(ch) => Err(ch),
+/// None => Ok(()),
+/// }
+/// }
+/// }
+/// ```
+pub trait Invariant: Sized {
+ /// The type returned in the event that an invariant is broken.
+ ///
+ /// When formatting, `Error` should not be capitalized and should not end
+ /// with a period.
+ type Error: fmt::Display;
+
+ /// Returns `Ok` if the string upholds the invariant, otherwise `Err`.
+ ///
+ /// This function is used internally in [`Check::from_buf`] and [`Ck::from_slice`].
+ fn check(slice: &str) -> Result<(), Self::Error>;
+}
+
+/// Conversion into a [`Ck`].
+pub trait IntoCk: Sized + AsRef<str> {
+ /// Returns a validated [`Ck`] borrowing from `self`.
+ ///
+ /// # Examples
+ ///
+ /// Creating an Rust ident containing `this`:
+ /// ```rust
+ /// use strck_ident::{IntoCk, rust::Ident};
+ ///
+ /// let this_ident: &Ident = "this".ck().unwrap();
+ /// ```
+ fn ck<I: Invariant>(&self) -> Result<&Ck<I>, I::Error>;
+}
+
+impl<T: AsRef<str>> IntoCk for T {
+ fn ck<I: Invariant>(&self) -> Result<&Ck<I>, I::Error> {
+ Ck::from_slice(self.as_ref())
+ }
+}
+
+/// Conversion into a [`Check`].
+pub trait IntoCheck: Sized + AsRef<str> + 'static {
+ /// Returns a validated [`Check`] owning `self`.
+ ///
+ /// Note that [`Check`] uses the input of [`IntoCheck::check`] as its backing
+ /// storage, meaning that `"this".check()` will return a `Check<I, &'static str>`.
+ /// Although this is technically valid, it's _strongly_ recommended to use
+ /// [`Ck`] for string slices instead to avoid confusion.
+ ///
+ /// # Examples
+ ///
+ /// Creating a Unicode ident from a formatted string:
+ /// ```rust
+ /// use strck_ident::{Check, Ck, IntoCheck, unicode::UnicodeIdent};
+ ///
+ /// fn wrapper_name(name: &Ck<UnicodeIdent>) -> Check<UnicodeIdent> {
+ /// format!("lil_{name}").check().unwrap()
+ /// }
+ /// ```
+ fn check<I: Invariant>(self) -> Result<Check<I, Self>, I::Error>;
+}
+
+impl<T: AsRef<str> + 'static> IntoCheck for T {
+ fn check<I: Invariant>(self) -> Result<Check<I, Self>, I::Error> {
+ Check::from_buf(self)
+ }
+}
+
+// impl Check
+
+impl<I: Invariant, B: AsRef<str>> Check<I, B> {
+ /// Returns an `Ok` if the buffer upholds the invariants, otherwise `Err`.
+ pub fn from_buf(buf: B) -> Result<Self, I::Error> {
+ I::check(buf.as_ref())?;
+
+ // SAFETY: invariants are upheld.
+ unsafe { Ok(Self::from_buf_unchecked(buf)) }
+ }
+
+ /// Create a new [`Check`] without validating the buffer.
+ ///
+ /// # Safety
+ ///
+ /// The buffer must contain a valid string.
+ pub unsafe fn from_buf_unchecked(buf: B) -> Self {
+ Check {
+ _marker: marker::PhantomData,
+ buf,
+ }
+ }
+
+ /// Returns a [`&Ck`](Ck) that borrows from `self`.
+ pub fn as_ck(&self) -> &Ck<I> {
+ // SAFETY: `self` has the same invariants as `&Ck<I>`.
+ unsafe { Ck::from_str_unchecked(self.buf.as_ref()) }
+ }
+
+ /// Returns the inner representation.
+ pub fn into_inner(self) -> B {
+ self.buf
+ }
+}
+
+impl<I, B> fmt::Debug for Check<I, B>
+where
+ I: Invariant,
+ B: AsRef<str> + fmt::Debug,
+{
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ fmt::Debug::fmt(&self.buf, f)
+ }
+}
+
+impl<I, B1, B2> PartialEq<Check<I, B2>> for Check<I, B1>
+where
+ I: Invariant,
+ B1: AsRef<str>,
+ B2: AsRef<str>,
+{
+ fn eq(&self, other: &Check<I, B2>) -> bool {
+ self == other
+ }
+}
+
+impl<I, B1, B2> PartialOrd<Check<I, B2>> for Check<I, B1>
+where
+ I: Invariant,
+ B1: AsRef<str>,
+ B2: AsRef<str>,
+{
+ fn partial_cmp(&self, other: &Check<I, B2>) -> Option<cmp::Ordering> {
+ self.as_ck().partial_cmp(other.as_ck())
+ }
+}
+
+impl<I: Invariant, B: AsRef<str>> Eq for Check<I, B> {}
+
+impl<I: Invariant, B: AsRef<str>> Ord for Check<I, B> {
+ fn cmp(&self, other: &Self) -> cmp::Ordering {
+ self.as_ck().cmp(other.as_ck())
+ }
+}
+
+impl<I: Invariant, B: AsRef<str>> hash::Hash for Check<I, B> {
+ fn hash<H: hash::Hasher>(&self, state: &mut H) {
+ self.as_str().hash(state);
+ }
+}
+
+impl<I: Invariant, B: AsRef<str>> ops::Deref for Check<I, B> {
+ type Target = Ck<I>;
+
+ fn deref(&self) -> &Self::Target {
+ self.as_ck()
+ }
+}
+
+impl<I: Invariant, B: AsRef<str>> AsRef<Ck<I>> for Check<I, B> {
+ fn as_ref(&self) -> &Ck<I> {
+ self.as_ck()
+ }
+}
+
+impl<I: Invariant, B: AsRef<str>> AsRef<str> for Check<I, B> {
+ fn as_ref(&self) -> &str {
+ self.as_str()
+ }
+}
+
+impl<I: Invariant, B: AsRef<str>> borrow::Borrow<Ck<I>> for Check<I, B> {
+ fn borrow(&self) -> &Ck<I> {
+ self.as_ck()
+ }
+}
+
+impl<I: Invariant, B: AsRef<str>> fmt::Display for Check<I, B> {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ fmt::Display::fmt(self.as_str(), f)
+ }
+}
+
+impl<'a, I, B> From<&'a Ck<I>> for Check<I, B>
+where
+ I: Invariant,
+ B: AsRef<str> + From<&'a str>,
+{
+ fn from(check: &'a Ck<I>) -> Self {
+ check.to_check()
+ }
+}
+
+impl<I, B> str::FromStr for Check<I, B>
+where
+ I: Invariant,
+ for<'a> B: AsRef<str> + From<&'a str>,
+{
+ type Err = I::Error;
+
+ fn from_str(s: &str) -> Result<Self, Self::Err> {
+ Ok(s.ck()?.to_check())
+ }
+}
+
+// impl Ck
+
+impl<I: Invariant> Ck<I> {
+ /// Returns an `Ok` if the `&str` upholds the invariants, otherwise `Err`.
+ pub fn from_slice(slice: &str) -> Result<&Self, I::Error> {
+ I::check(slice)?;
+
+ // SAFETY: invariants are upheld.
+ unsafe { Ok(Self::from_str_unchecked(slice)) }
+ }
+
+ /// Create a new [`&Ck`](Ck) without validating the `&str`.
+ ///
+ /// # Safety
+ ///
+ /// The string must be valid.
+ pub unsafe fn from_str_unchecked(slice: &str) -> &Self {
+ // SAFETY: `Ck` has the same ABI as `str` by `#[repr(transparent)]`.
+ core::mem::transmute(slice)
+ }
+
+ /// Returns an owned [`Check`] from `&self`.
+ pub fn to_check<'a, B>(&'a self) -> Check<I, B>
+ where
+ B: AsRef<str> + From<&'a str>,
+ {
+ // SAFETY: `self` has the same invariants as `Check<I, B>`.
+ unsafe { Check::from_buf_unchecked(self.as_str().into()) }
+ }
+
+ /// Returns the `&str` representation.
+ pub fn as_str(&self) -> &str {
+ &self.slice
+ }
+}
+
+impl<I: Invariant> fmt::Debug for Ck<I> {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ fmt::Debug::fmt(&self.slice, f)
+ }
+}
+
+impl<I: Invariant> PartialEq for Ck<I> {
+ fn eq(&self, other: &Self) -> bool {
+ self.as_str() == other.as_str()
+ }
+}
+
+impl<I: Invariant> PartialOrd for Ck<I> {
+ fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
+ self.slice.partial_cmp(&other.slice)
+ }
+}
+
+impl<I: Invariant> Eq for Ck<I> {}
+
+impl<I: Invariant> Ord for Ck<I> {
+ fn cmp(&self, other: &Self) -> cmp::Ordering {
+ self.as_str().cmp(other.as_str())
+ }
+}
+
+impl<I: Invariant> hash::Hash for Ck<I> {
+ fn hash<H: hash::Hasher>(&self, state: &mut H) {
+ self.as_str().hash(state);
+ }
+}
+
+impl<I: Invariant> AsRef<str> for Ck<I> {
+ fn as_ref(&self) -> &str {
+ self.as_str()
+ }
+}
+
+impl<I: Invariant> borrow::Borrow<str> for Ck<I> {
+ fn borrow(&self) -> &str {
+ self.as_str()
+ }
+}
+
+impl<I: Invariant> ToOwned for Ck<I> {
+ type Owned = Check<I>;
+
+ fn to_owned(&self) -> Self::Owned {
+ self.to_check()
+ }
+}
+
+impl<I: Invariant> fmt::Display for Ck<I> {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ fmt::Display::fmt(self.as_str(), f)
+ }
+}
+
+impl<'a, I: Invariant, B: AsRef<str>> From<&'a Check<I, B>> for &'a Ck<I> {
+ fn from(check: &'a Check<I, B>) -> Self {
+ check.as_ck()
+ }
+}
+
+impl<'a, I: Invariant> TryFrom<&'a str> for &'a Ck<I> {
+ type Error = I::Error;
+
+ fn try_from(slice: &'a str) -> Result<Self, Self::Error> {
+ Ck::from_slice(slice)
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ /// Test invariant.
+ struct NoInvariant;
+
+ impl Invariant for NoInvariant {
+ type Error = core::convert::Infallible;
+
+ fn check(_slice: &str) -> Result<(), Self::Error> {
+ Ok(())
+ }
+ }
+
+ #[test]
+ fn test_debug_impl() {
+ let this = "this".ck::<NoInvariant>().unwrap();
+ let fmt_debug = format!("{:?}", this);
+
+ assert_eq!(fmt_debug, "\"this\"");
+ }
+}
diff --git a/third_party/rust/strck/src/partial_eq.rs b/third_party/rust/strck/src/partial_eq.rs
new file mode 100644
index 0000000000..22dfd288b6
--- /dev/null
+++ b/third_party/rust/strck/src/partial_eq.rs
@@ -0,0 +1,94 @@
+//! Permutations of `PartialEq` between `Ck`, `Check`, `str`, and `String`.
+use crate::{Check, Ck, Invariant};
+
+// `Check`s don't need to have the same backing storage to be equal.
+impl<'a, I, B1, B2> PartialEq<Check<I, B2>> for &'a Check<I, B1>
+where
+ I: Invariant,
+ B1: AsRef<str>,
+ B2: AsRef<str>,
+{
+ fn eq(&self, other: &Check<I, B2>) -> bool {
+ self.as_str() == other.as_str()
+ }
+}
+
+impl<'a, I, B1, B2> PartialEq<&'a Check<I, B2>> for Check<I, B1>
+where
+ I: Invariant,
+ B1: AsRef<str>,
+ B2: AsRef<str>,
+{
+ fn eq(&self, other: &&'a Check<I, B2>) -> bool {
+ self.as_str() == other.as_str()
+ }
+}
+
+macro_rules! impl_partial_eq {
+ (<I> $a:ty = $b:ty) => {
+ impl<I: Invariant> PartialEq<$a> for $b {
+ fn eq(&self, other: &$a) -> bool {
+ AsRef::<str>::as_ref(self) == AsRef::<str>::as_ref(other)
+ }
+ }
+ };
+ (<I, B> $a:ty = $b:ty) => {
+ impl<I: Invariant, B: AsRef<str>> PartialEq<$a> for $b {
+ fn eq(&self, other: &$a) -> bool {
+ AsRef::<str>::as_ref(self) == AsRef::<str>::as_ref(other)
+ }
+ }
+ };
+ (<$lt:lifetime, I> $a:ty = $b:ty) => {
+ impl<$lt, I: Invariant> PartialEq<$a> for $b {
+ fn eq(&self, other: &$a) -> bool {
+ AsRef::<str>::as_ref(self) == AsRef::<str>::as_ref(other)
+ }
+ }
+ };
+ (<$lt:lifetime, I, B> $a:ty = $b:ty) => {
+ impl<$lt, I: Invariant, B: AsRef<str>> PartialEq<$a> for $b {
+ fn eq(&self, other: &$a) -> bool {
+ AsRef::<str>::as_ref(self) == AsRef::<str>::as_ref(other)
+ }
+ }
+ };
+}
+
+impl_partial_eq!(<'a, I> Ck<I> = &'a Ck<I>);
+impl_partial_eq!(<'a, I> &'a Ck<I> = Ck<I>);
+
+impl_partial_eq!(<I, B> Ck<I> = Check<I, B>);
+impl_partial_eq!(<I, B> Check<I, B> = Ck<I>);
+impl_partial_eq!(<'a, I, B> &'a Ck<I> = Check<I, B>);
+impl_partial_eq!(<'a, I, B> Check<I, B> = &'a Ck<I>);
+impl_partial_eq!(<'a, I, B> Ck<I> = &'a Check<I, B>);
+impl_partial_eq!(<'a, I, B> &'a Check<I, B> = Ck<I>);
+
+impl_partial_eq!(<I, B> str = Check<I, B>);
+impl_partial_eq!(<I, B> Check<I, B> = str);
+impl_partial_eq!(<'a, I, B> &'a str = Check<I, B>);
+impl_partial_eq!(<'a, I, B> Check<I, B> = &'a str);
+impl_partial_eq!(<'a, I, B> str = &'a Check<I, B>);
+impl_partial_eq!(<'a, I, B> &'a Check<I, B> = str);
+
+impl_partial_eq!(<I, B> String = Check<I, B>);
+impl_partial_eq!(<I, B> Check<I, B> = String);
+impl_partial_eq!(<'a, I, B> &'a String = Check<I, B>);
+impl_partial_eq!(<'a, I, B> Check<I, B> = &'a String);
+impl_partial_eq!(<'a, I, B> String = &'a Check<I, B>);
+impl_partial_eq!(<'a, I, B> &'a Check<I, B> = String);
+
+impl_partial_eq!(<I> str = Ck<I>);
+impl_partial_eq!(<I> Ck<I> = str);
+impl_partial_eq!(<'a, I> &'a str = Ck<I>);
+impl_partial_eq!(<'a, I> Ck<I> = &'a str);
+impl_partial_eq!(<'a, I> str = &'a Ck<I>);
+impl_partial_eq!(<'a, I> &'a Ck<I> = str);
+
+impl_partial_eq!(<I> String = Ck<I>);
+impl_partial_eq!(<I> Ck<I> = String);
+impl_partial_eq!(<'a, I> &'a String = Ck<I>);
+impl_partial_eq!(<'a, I> Ck<I> = &'a String);
+impl_partial_eq!(<'a, I> String = &'a Ck<I>);
+impl_partial_eq!(<'a, I> &'a Ck<I> = String);
diff --git a/third_party/rust/strck/src/serde.rs b/third_party/rust/strck/src/serde.rs
new file mode 100644
index 0000000000..52cbc4670b
--- /dev/null
+++ b/third_party/rust/strck/src/serde.rs
@@ -0,0 +1,78 @@
+//! Implement [`Serialize`] and [`Deserialize`] for [`Check`] and [`Ck`].
+
+use crate::{Check, Ck, Invariant};
+use serde::de::{Deserialize, Deserializer, Error};
+use serde::ser::{Serialize, Serializer};
+
+impl<I: Invariant, B: AsRef<str>> Serialize for Check<I, B> {
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ serializer.serialize_str(self.as_str())
+ }
+}
+
+impl<I: Invariant> Serialize for Ck<I> {
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ serializer.serialize_str(self.as_str())
+ }
+}
+
+impl<'de, I, B> Deserialize<'de> for Check<I, B>
+where
+ I: Invariant,
+ B: AsRef<str> + Deserialize<'de>,
+{
+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ let buf = Deserialize::deserialize(deserializer)?;
+
+ Check::from_buf(buf).map_err(Error::custom)
+ }
+}
+
+impl<'de: 'a, 'a, I: Invariant> Deserialize<'de> for &'a Ck<I> {
+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ let slice = Deserialize::deserialize(deserializer)?;
+
+ Ck::from_slice(slice).map_err(Error::custom)
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use serde::{Deserialize, Serialize};
+ use strck_ident::{Ident, IntoCk};
+
+ #[derive(Serialize, Deserialize, Debug, PartialEq)]
+ struct Player<'a> {
+ #[serde(borrow)]
+ username: &'a Ident,
+ level: u32,
+ }
+
+ #[test]
+ fn test_zero_copy_deserialization() {
+ let qnn = Player {
+ username: "qnn".ck().unwrap(),
+ level: 100,
+ };
+
+ fn get() -> Player<'static> {
+ let ser = r#"{"username":"qnn","level":100}"#;
+ serde_json::from_str(&ser).unwrap()
+ }
+
+ let de = get();
+ assert_eq!(qnn, de);
+ }
+}