cast_wd 0.0.5

这是一个类型转换的package
Documentation
//! 类型转换为&[u8]
//! author:wangdong
//! time:2020-09-30 14:13:56
pub trait Cast{
    fn bytes(&self)->&[u8]{
        &[]
    }
}
/// 转换类型为&[u8]
/// 
/// #Examples
/// 
/// ```
/// let s = "hello world";
/// let a = bytes!(s);
/// 
/// ```
#[macro_export]
macro_rules! r#bytes {
    ($value:expr) => {
        $crate::bytes::bytes(&$value)
    };
}

/// 转换类型为&[u8]
/// 
/// #Examples
/// 
/// ```
/// let s = "hello world";
/// let a = bytes(s);
/// 
/// ```
#[allow(dead_code)]
pub fn bytes<'a,T:Cast>(value:&'a T)->&'a[u8]{
    value.bytes()
}

impl Cast for String{
    fn bytes(&self)->&[u8]{
        self.as_bytes()
    }
}
impl Cast for &str{
    fn bytes(&self)->&[u8]{
        self.as_bytes()
    }
}

impl Cast for Vec<u8>{
    fn bytes(&self)->&[u8]{
        self.as_slice()
    }
}