Crate string_newtype

source ·
Expand description

§string-newtype: New Type idiom helper for string-like types

string-newtype is a helper library for using the newtype idiom with string-like types, including newtyped string slices.

§Usage

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);

Structs§

Type Aliases§