Crate shrinkwraprs [] [src]

shrinkwraprs

Making wrapper types allows us to give more compile-time guarantees about our code being correct:

Be careful when using this code, it's not being tested!
// Now we can't mix up widths and heights; the compiler will yell at us!
struct Width(i64);
struct Height(i64);

But... they're kind of a pain to work with. If you ever need to get at that wrapped i64, you need to constantly pattern-match back and forth to wrap and unwrap the values.

shrinkwraprs aims to alleviate this pain by allowing you to derive implementations of various conversion traits by attaching #[derive(Shrinkwrap)].

Traits implemented

Currently, shrinkwraprs derives the following traits for all structs:

  • AsRef<InnerType>
  • AsMut<InnerType>
  • Borrow<InnerType>
  • BorrowMut<InnerType>
  • Deref<Target=InnerType>
  • DerefMut<Target=InnerType>

Cool, how do I use it?

Be careful when using this code, it's not being tested!
#[macro_use] extern crate shrinkwraprs;

#[derive(Shrinkwrap)]
struct Email(String);

fn main() {
  let email = Email("chiya+snacks@natsumeya.jp".into());

  let is_discriminated_email =
    (*email).contains("+");  // Woohoo, we can use the email like a string!

  /* ... */
}

Functions

shrinkwrap