sting 0.1.1

Smaller alternatives to Cow<'static, {str,[u8]}>
Documentation
#![cfg_attr(not(feature="std"), no_std)]
use core::ptr;
use core::ops::Deref;

use sting::{Sting, Sluice};
/// The maximum width of string that will be stored inline.
const INLINE: &str = if cfg!(target_pointer_width="32") { "Hello W" } else { "Hello World Hel" };
/// The minimum width of string that will overflow the inline storage
const OVERFLOW: &str = if cfg!(target_pointer_width="32") { "Hello Wo" } else { "Hello World Hell"};

#[test]
fn static_strings() {
  let st = Sting::from_static(INLINE);
  assert!(ptr::eq(st.deref() as *const str, INLINE), "reuses the str passed in's address");
  assert_eq!(st.len(), INLINE.len(), "retains the correct size");
  assert_eq!(st.deref(), INLINE, "retains the correct contents");
  let st = Sting::from_static(OVERFLOW);
  assert!(ptr::eq(st.deref() as *const str, OVERFLOW), "reuses the str passed in's address");
  assert_eq!(st.len(), OVERFLOW.len(), "retains the correct size");
  assert_eq!(st.deref(), OVERFLOW, "retains the correct contents");
  assert!(Sting::from_static("").is_empty());
}

#[test]
fn inline_strings() {
  let st = Sting::copy(INLINE);
  assert!(
    ptr::eq(st.deref() as *const str as *const u8, &st as *const Sting as *const u8),
    "uses the inline storage"
  );
  assert_eq!(st.len(), INLINE.len(), "retains the correct size");
  assert_eq!(st.deref(), INLINE, "retains the correct contents");
  assert!(Sting::copy("").is_empty());
}

#[test]
fn boxed_strings() {
  let st = Sting::copy(OVERFLOW);
  assert!(
    !ptr::eq(st.deref() as *const str as *const u8, &st as *const Sting as *const u8),
    "does not use the inline storage"
  );
  assert!(
    !ptr::eq(st.deref() as *const str, OVERFLOW as *const str),
    "does not reuse the static storage"
  );
  assert_eq!(st.len(), OVERFLOW.len(), "retains the correct size");
  assert_eq!(st.deref(), OVERFLOW, "retains the correct contents");
}

#[test]
fn static_sluices() {
  let sl = Sluice::from_static(INLINE.as_bytes());
  assert!(ptr::eq(sl.deref() as *const [u8], INLINE.as_bytes()), "reuses the str passed in's address");
  assert_eq!(sl.len(), INLINE.len(), "retains the correct size");
  assert_eq!(sl.deref(), INLINE.as_bytes(), "retains the correct contents");
  let sl = Sluice::from_static(OVERFLOW.as_bytes());
  assert!(ptr::eq(sl.deref() as *const [u8], OVERFLOW.as_bytes()), "reuses the str passed in's address");
  assert_eq!(sl.len(), OVERFLOW.len(), "retains the correct size");
  assert_eq!(sl.deref(), OVERFLOW.as_bytes(), "retains the correct contents");
  assert!(Sluice::from_static(b"").is_empty());
}

#[test]
fn inline_sluices() {
  let sl = Sluice::copy(INLINE.as_bytes());
  assert!(
    ptr::eq(sl.deref() as *const [u8] as *const u8, &sl as *const Sluice as *const u8),
    "uses the inline storage"
  );
  assert_eq!(sl.len(), INLINE.len(), "retains the correct size");
  assert_eq!(sl.deref(), INLINE.as_bytes(), "retains the correct contents");
  assert!(Sting::copy("").is_empty());
  for i in 0..sting::MAX_SIZE.ilog2() {
    let sluice = Sluice::zeroed(i as usize);
    let sl = sluice.deref();
    assert_eq!(sl.len(), i as usize, "retains the correct size");
    for b in sl {
      assert_eq!(*b, 0)
    }
  }

}

#[test]
fn boxed_sluices() {
  let sluice = Sluice::zeroed(OVERFLOW.len());
  let sl = sluice.deref();
  assert_eq!(sl.len(), OVERFLOW.len(), "retains the correct size");
  for b in sl {
    assert_eq!(*b, 0)
  }
}