Crates.io | string-newtype |
lib.rs | string-newtype |
version | 0.1.1 |
source | src |
created_at | 2024-04-21 15:48:07.69401 |
updated_at | 2024-04-22 18:41:36.57477 |
description | New Type idiom helper for string-like types |
homepage | https://github.com/aborgna@string-newtype |
repository | https://github.com/aborgna@string-newtype |
max_upload_size | |
id | 1215480 |
size | 34,862 |
string-newtype
is a helper library for using the newtype idiom with string-like types, including newtyped string slices.
Define an empty enum as a marker for your type, and add aliases based on it:
// A marker type, can be anything.
enum S {}
// The newtype definitions.
// `StringBuf` acts as a `String`, while `StringRef` acts as a `str`.
type SBuf = StringBuf<S>;
type SRef = StringRef<S>;
// Define functions that only accept the newtype.
fn my_func(owned: SBuf, reference: &SRef) {
// ...
}
// Only the newtype can be passed to the function.
let s: SBuf = "hello".into();
my_func(s.clone(), &s);
The crate also defines SmolStrBuf
and SmolStrRef
types, which are newtypes
for smol_str::SmolStr
.
smol_str
Enables newtypes for smol_str::SmolStr
s.
serde
Enables serialization and deserialization implementations.
See DEVELOPMENT.md for instructions on setting up the development environment.
This project is licensed under Apache License, Version 2.0 (LICENSE or http://www.apache.org/licenses/LICENSE-2.0).