| 1 |
[![github-img]][github-url] [![crates-img]][crates-url] [![docs-img]][docs-url] |
| 2 |
|
| 3 |
Checked owned and borrowed strings. |
| 4 |
|
| 5 |
# Overview |
| 6 |
|
| 7 |
The Rust standard library provides the `String` and `str` types, which wrap |
| 8 |
`Vec<u8>` and `[u8]` respectively, with the invariant that the contents |
| 9 |
are valid UTF-8. |
| 10 |
|
| 11 |
This crate abstracts the idea of type-level invariants on strings by |
| 12 |
introducing the immutable `Check` and `Ck` types, where the invariants |
| 13 |
are determined by a generic `Invariant` type parameter. Implementing |
| 14 |
the `Invariant` trait is left to other crates, such as [`strck_ident`]. |
| 15 |
|
| 16 |
"strck" comes from "str check", similar to how rustc has typeck and |
| 17 |
borrowck for type check and borrow check respectively. |
| 18 |
|
| 19 |
See [the documentation][docs-url] for more details. |
| 20 |
|
| 21 |
# Motivation |
| 22 |
|
| 23 |
Libraries working with string-like types with certain properties, like identifiers, |
| 24 |
quickly become confusing as `&str` and `String` begin to pollute type signatures |
| 25 |
everywhere. One solution is to manually implement an owned checked string type |
| 26 |
like `syn::Ident` to disambiguate the type signatures and validate the string. |
| 27 |
The downside is that new values cannot be created without allocation, |
| 28 |
which is unnecessary when only a borrowed version is required. |
| 29 |
|
| 30 |
`strck` solves this issue by providing a checked borrowed string type, `Ck`, |
| 31 |
alongside a checked owned string type, `Check`. These serve as thin wrappers |
| 32 |
around `str` and `String`[^1] respectively, and prove at the type level that |
| 33 |
the contents satisfy the `Invariant` that the wrapper is generic over. |
| 34 |
|
| 35 |
[^1]: `Check` can actually be backed by any `'static + AsRef<str>` type, |
| 36 |
but `String` is the default. |
| 37 |
|
| 38 |
# Use cases |
| 39 |
|
| 40 |
### Checked strings without allocating |
| 41 |
|
| 42 |
The main benefit `strck` offers is validating borrowed strings via the |
| 43 |
`Ck` type without having to allocate in the result. |
| 44 |
|
| 45 |
```rust |
| 46 |
use strck_ident::{Ck, IntoCk, rust::RustIdent}; |
| 47 |
|
| 48 |
let this_ident: &Ck<RustIdent> = "this".ck().unwrap(); |
| 49 |
``` |
| 50 |
|
| 51 |
### Checked zero-copy deserialization |
| 52 |
|
| 53 |
When the `serde` feature flag is enabled, `Ck`s can be used to perform |
| 54 |
checked zero-copy deserialization, which requires the |
| 55 |
`#[serde(borrow)]` attribute. |
| 56 |
|
| 57 |
```rust |
| 58 |
use strck_ident::{Ck, unicode::UnicodeIdent}; |
| 59 |
|
| 60 |
#[derive(Serialize, Deserialize)] |
| 61 |
struct Player<'a> { |
| 62 |
#[serde(borrow)] |
| 63 |
username: &'a Ck<UnicodeIdent>, |
| 64 |
level: u32, |
| 65 |
} |
| 66 |
``` |
| 67 |
|
| 68 |
Note that this code sample explicitly uses `Ck<UnicodeIdent>` to demonstrate |
| 69 |
that the type is a `Ck`. However, `strck_ident` provides `Ident` as an |
| 70 |
alias for `Ck<UnicodeIdent>`, which should be used in practice. |
| 71 |
|
| 72 |
|
| 73 |
### Infallible parsing |
| 74 |
|
| 75 |
For types where string validation is relatively cheap but parsing is costly |
| 76 |
and fallible, `strck` can be used with a custom `Invariant` as an input to |
| 77 |
make an infallible parsing function. |
| 78 |
|
| 79 |
# Postfix construction with `IntoCk` and `IntoCheck` |
| 80 |
|
| 81 |
This crate exposes two helper traits, `IntoCk` and `IntoCheck`. When in |
| 82 |
scope, the `.ck()` and `.check()` functions can be used to create |
| 83 |
`Ck`s and `Check`s respectively: |
| 84 |
|
| 85 |
```rust |
| 86 |
use strck_ident::{IntoCheck, IntoCk, unicode::UnicodeIdent}; |
| 87 |
|
| 88 |
let this_ident = "this".ck::<UnicodeIdent>().unwrap(); |
| 89 |
let this_foo_ident = format!("{}_foo", this_ident).check::<UnicodeIdent>().unwrap(); |
| 90 |
``` |
| 91 |
|
| 92 |
# Documentation |
| 93 |
|
| 94 |
See the [crate-level documentation][docs-url] for more details. |
| 95 |
|
| 96 |
[`strck_ident`]: https://docs.rs/strck_ident |
| 97 |
|
| 98 |
[github-url]: https://github.com/QnnOkabayashi/strck |
| 99 |
[crates-url]: https://crates.io/crates/strck |
| 100 |
[docs-url]: https://docs.rs/strck |
| 101 |
[github-img]: https://img.shields.io/badge/github-8da0cb?style=for-the-badge&labelColor=555555&logo=github |
| 102 |
[crates-img]: https://img.shields.io/badge/crates.io-fc8d62?style=for-the-badge&labelColor=555555&logo=rust |
| 103 |
[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 |