blob: 49c02d7632054ff68135053083b52012d972639a [file] [log] [blame]
Haibo Huang52aa7852020-07-10 20:23:55 -07001#![cfg(feature = "bytes")]
2
Chih-Hung Hsiehcfc3a232020-06-10 20:13:05 -07003use std::fmt;
4use std::ops::Deref;
5use std::str;
6
7use bytes::Bytes;
8
Haibo Huang52aa7852020-07-10 20:23:55 -07009use crate::clear::Clear;
Chih-Hung Hsiehcfc3a232020-06-10 20:13:05 -070010
11/// Thin wrapper around `Bytes` which guarantees that bytes are valid UTF-8 string.
Haibo Huang52aa7852020-07-10 20:23:55 -070012/// Should be API-compatible to `String`.
Chih-Hung Hsiehcfc3a232020-06-10 20:13:05 -070013#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
14pub struct Chars(Bytes);
15
16impl Chars {
17 /// New empty object.
18 pub fn new() -> Chars {
19 Chars(Bytes::new())
20 }
21
22 /// Try convert from `Bytes`
23 pub fn from_bytes(bytes: Bytes) -> Result<Chars, str::Utf8Error> {
24 str::from_utf8(&bytes)?;
25
26 Ok(Chars(bytes))
27 }
28
29 /// Len in bytes.
30 pub fn len(&self) -> usize {
31 self.0.len()
32 }
33
34 /// Self-explanatory
35 pub fn is_empty(&self) -> bool {
36 self.0.is_empty()
37 }
38}
39
40impl<'a> From<&'a str> for Chars {
41 fn from(src: &'a str) -> Chars {
42 Chars(Bytes::copy_from_slice(src.as_bytes()))
43 }
44}
45
46impl From<String> for Chars {
47 fn from(src: String) -> Chars {
48 Chars(Bytes::from(src))
49 }
50}
51
Haibo Huang52aa7852020-07-10 20:23:55 -070052impl Into<String> for Chars {
53 fn into(self) -> String {
54 // This is safe because `Chars` is guaranteed to store a valid UTF-8 string
55 unsafe { String::from_utf8_unchecked(self.0.as_ref().to_owned()) }
56 }
57}
58
Chih-Hung Hsiehcfc3a232020-06-10 20:13:05 -070059impl Default for Chars {
60 fn default() -> Self {
61 Chars::new()
62 }
63}
64
65impl Deref for Chars {
66 type Target = str;
67
68 fn deref(&self) -> &str {
Haibo Huang52aa7852020-07-10 20:23:55 -070069 // This is safe because `Chars` is guaranteed to store a valid UTF-8 string
Chih-Hung Hsiehcfc3a232020-06-10 20:13:05 -070070 unsafe { str::from_utf8_unchecked(&self.0) }
71 }
72}
73
74impl Clear for Chars {
75 fn clear(&mut self) {
76 self.0.clear();
77 }
78}
79
80impl fmt::Display for Chars {
81 #[inline]
82 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
83 fmt::Display::fmt(&**self, f)
84 }
85}
86
87impl fmt::Debug for Chars {
88 #[inline]
89 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
90 fmt::Debug::fmt(&**self, f)
91 }
92}
93
94#[cfg(test)]
95mod test {
96 use super::Chars;
97
98 #[test]
99 fn test_display_and_debug() {
100 let s = "test";
101 let string: String = s.into();
102 let chars: Chars = s.into();
103
104 assert_eq!(format!("{}", string), format!("{}", chars));
105 assert_eq!(format!("{:?}", string), format!("{:?}", chars));
106 }
107}