Skip to main content

ntex_amqp_codec/types/
symbol.rs

1use std::{borrow, str};
2
3use ntex_bytes::ByteString;
4
5use super::Str;
6
7#[derive(Debug, Clone, Eq, PartialEq, Hash)]
8pub struct Symbol(pub Str);
9
10impl Symbol {
11    pub const fn from_static(s: &'static str) -> Symbol {
12        Symbol(Str::from_static(s))
13    }
14
15    pub fn from_slice(s: &str) -> Symbol {
16        Symbol(Str(ByteString::from(s)))
17    }
18
19    pub fn as_bytes(&self) -> &[u8] {
20        self.0.as_bytes()
21    }
22
23    pub fn as_str(&self) -> &str {
24        self.0.as_str()
25    }
26
27    pub fn to_bytes_str(&self) -> ByteString {
28        self.0.to_bytes_str()
29    }
30
31    pub fn len(&self) -> usize {
32        self.0.len()
33    }
34}
35
36impl Default for Symbol {
37    fn default() -> Symbol {
38        Symbol::from_static("")
39    }
40}
41
42impl From<&'static str> for Symbol {
43    fn from(s: &'static str) -> Symbol {
44        Symbol::from_static(s)
45    }
46}
47
48impl From<Str> for Symbol {
49    fn from(s: Str) -> Symbol {
50        Symbol(s)
51    }
52}
53
54impl From<std::string::String> for Symbol {
55    fn from(s: std::string::String) -> Symbol {
56        Symbol(Str::from(s))
57    }
58}
59
60impl From<ByteString> for Symbol {
61    fn from(s: ByteString) -> Symbol {
62        Symbol(Str(s))
63    }
64}
65
66impl borrow::Borrow<str> for Symbol {
67    fn borrow(&self) -> &str {
68        self.as_str()
69    }
70}
71
72impl PartialEq<str> for Symbol {
73    fn eq(&self, other: &str) -> bool {
74        self.0 == *other
75    }
76}
77
78#[derive(Debug, Clone, PartialEq, Eq, Hash)]
79pub struct StaticSymbol(pub &'static str);
80
81impl StaticSymbol {
82    pub const fn new(s: &'static str) -> StaticSymbol {
83        StaticSymbol(s)
84    }
85}
86
87impl From<&'static str> for StaticSymbol {
88    fn from(s: &'static str) -> StaticSymbol {
89        StaticSymbol(s)
90    }
91}