use crate::*;
use core::{
hash::{Hash, Hasher},
str::FromStr,
};
impl_for! {
From<String>:
fn from(str: String) -> Self {
Self::from(str.as_str())
}
}
impl_for! {
FromStr:
type Err = ();
fn from_str(str: &str) -> Result<Self, Self::Err> {
Ok(Self::from(str))
}
}
impl_for! {
From<&str>:
fn from(str: &str) -> Self {
Self::from_str(str).unwrap_or_else(|_| panic!("{}::from(): cannot store {} characters",
Self::type_name(),
str.len()
))
}
}
impl_for! {
Hash:
fn hash<H: Hasher>(&self, state: &mut H) {
self.str.hash(state);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_from_string() {
let s: SlimStringlet<4> = String::from("hey").into();
assert_eq!(s.as_ref(), "hey");
}
#[test]
fn test_from_long_str() {
let s: VarStringlet<16> = "Rustacean".into();
assert_eq!(&s, "Rustacean");
}
#[test]
#[should_panic]
fn test_panics_when_too_long() {
let _s: VarStringlet<2> = "hello world".into();
}
#[test]
fn test_from_str() {
let s = SlimStringlet::<8>::from("hello");
assert_eq!(s.as_ref(), "hello");
}
}