amqp_codec/types/
symbol.rs1use std::{borrow, str};
2
3use bytestring::ByteString;
4
5use super::Str;
6
7#[derive(Debug, Clone, Eq, PartialEq, Hash, Display)]
8pub struct Symbol(pub Str);
9
10impl Symbol {
11 pub fn from_slice(s: &str) -> Symbol {
12 Symbol(Str::ByteStr(ByteString::from(s)))
13 }
14
15 pub fn as_bytes(&self) -> &[u8] {
16 self.0.as_bytes()
17 }
18
19 pub fn as_str(&self) -> &str {
20 self.0.as_str()
21 }
22
23 pub fn to_bytes_str(&self) -> ByteString {
24 self.0.to_bytes_str()
25 }
26
27 pub fn len(&self) -> usize {
28 self.0.len()
29 }
30}
31
32impl From<&'static str> for Symbol {
33 fn from(s: &'static str) -> Symbol {
34 Symbol(Str::Static(s))
35 }
36}
37
38impl From<Str> for Symbol {
39 fn from(s: Str) -> Symbol {
40 Symbol(s)
41 }
42}
43
44impl From<std::string::String> for Symbol {
45 fn from(s: std::string::String) -> Symbol {
46 Symbol(Str::from(s))
47 }
48}
49
50impl From<ByteString> for Symbol {
51 fn from(s: ByteString) -> Symbol {
52 Symbol(Str::ByteStr(s))
53 }
54}
55
56impl borrow::Borrow<str> for Symbol {
57 fn borrow(&self) -> &str {
58 self.as_str()
59 }
60}
61
62impl PartialEq<str> for Symbol {
63 fn eq(&self, other: &str) -> bool {
64 self.0 == *other
65 }
66}
67
68#[derive(Debug, Clone, PartialEq, Eq, Hash, Display)]
69pub struct StaticSymbol(pub &'static str);
70
71impl From<&'static str> for StaticSymbol {
72 fn from(s: &'static str) -> StaticSymbol {
73 StaticSymbol(s)
74 }
75}