string_newtype/
lib.rs

1//! # string-newtype: New Type idiom helper for string-like types
2//!
3//!
4//! `string-newtype` is a helper library for using the [newtype idiom](https://doc.rust-lang.org/rust-by-example/generics/new_types.html) with string-like types, including newtyped string slices.
5//!
6//! # Usage
7//!
8//! Define an empty enum as a marker for your type, and add aliases based on it:
9//!
10//! ```rust
11//! # use string_newtype::{StringBuf, StringRef};
12//! // A marker type, can be anything.
13//! enum S {}
14//!
15//! // The newtype definitions.
16//! // `StringBuf` acts as a `String`, while `StringRef` acts as a `str`.
17//! type SBuf = StringBuf<S>;
18//! type SRef = StringRef<S>;
19//!
20//! // Define functions that only accept the newtype.
21//! fn my_func(owned: SBuf, reference: &SRef) {
22//!     // ...
23//! }
24//!
25//! // Only the newtype can be passed to the function.
26//! let s: SBuf = "hello".into();
27//! my_func(s.clone(), &s);
28//! ```
29
30mod core;
31#[cfg(feature = "smol_str")]
32mod smol_str;
33mod string;
34
35pub use crate::core::{NewtypeBuf, NewtypeRef};
36#[cfg(feature = "smol_str")]
37pub use crate::smol_str::{SmolStrBuf, SmolStrRef};
38pub use crate::string::{StringBuf, StringRef};