This commit is contained in:
Jun Kurihara 2022-07-28 19:46:03 +09:00
commit 4d590f328f
No known key found for this signature in database
GPG key ID: 48ADFD173ED22B03
8 changed files with 39 additions and 33 deletions

View file

@ -1,20 +1,22 @@
use bytes::{Buf, Bytes};
pub trait BytesName {
type Output;
fn to_byte_name(self) -> Self::Output;
fn to_ascii_lowercase_byte_name(self) -> Self::Output;
type Output: Buf;
fn to_bytes(self) -> Self::Output;
fn to_ascii_lowercase_bytes(self) -> Self::Output;
}
impl<T: Into<String>> BytesName for T {
type Output = Vec<u8>;
type Output = Bytes;
fn to_byte_name(self) -> Self::Output {
self.into().bytes().collect::<Vec<u8>>()
// Bytes::from(b)
fn to_bytes(self) -> Self::Output {
let b = self.into().bytes().collect::<Vec<u8>>();
Bytes::from(b)
}
fn to_ascii_lowercase_byte_name(self) -> Self::Output {
self.into().bytes().collect::<Vec<u8>>().to_ascii_lowercase()
// Bytes::from(b)
fn to_ascii_lowercase_bytes(self) -> Self::Output {
let b = self.into().bytes().collect::<Vec<u8>>().to_ascii_lowercase();
Bytes::from(b)
}
}
@ -24,10 +26,10 @@ mod tests {
#[test]
fn bytes_name_str_works() {
let s = "OK_string";
let bn = s.to_byte_name();
let bn_lc = s.to_ascii_lowercase_byte_name();
let bn = s.to_bytes();
let bn_lc = s.to_ascii_lowercase_bytes();
assert_eq!(Vec::from(s.as_bytes()), bn);
assert_eq!(Vec::from("ok_string"), bn_lc);
assert_eq!(Bytes::from(s.as_bytes()), bn);
assert_eq!(Bytes::from("ok_string"), bn_lc);
}
}