pci_info/error/
pci_info_error_string.rs1use std::{borrow::Cow, fmt::Debug, fmt::Display};
2
3type HeapPtr<T> = std::sync::Arc<T>;
7
8#[derive(Clone)]
44pub struct PciInfoErrorString(HeapPtr<Cow<'static, str>>);
45
46impl From<PciInfoErrorString> for String {
47 fn from(value: PciInfoErrorString) -> Self {
48 value.to_string()
49 }
50}
51
52impl<'a> From<&'a PciInfoErrorString> for &'a str {
53 fn from(value: &'a PciInfoErrorString) -> Self {
54 &value.0
55 }
56}
57
58impl PciInfoErrorString {
59 pub fn as_str(&self) -> &str {
60 &self.0
61 }
62}
63
64impl From<&'static str> for PciInfoErrorString {
65 fn from(value: &'static str) -> Self {
66 Self(HeapPtr::new(value.into()))
67 }
68}
69
70impl From<String> for PciInfoErrorString {
71 fn from(value: String) -> Self {
72 Self(HeapPtr::new(value.into()))
73 }
74}
75
76impl Display for PciInfoErrorString {
77 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
78 write!(f, "{}", self.0)
79 }
80}
81
82impl Debug for PciInfoErrorString {
83 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
84 write!(f, "\"{}\"", self.0)
85 }
86}
87
88#[cfg(test)]
89mod tests {
90 use super::PciInfoErrorString;
91
92 #[test]
93 fn pciinfoerrorstring_staticstr() {
94 let errs: PciInfoErrorString = "test".into();
95
96 let s: String = errs.to_string();
97 assert_eq!(s, "test");
98
99 let errs2 = errs.clone();
100 let s: String = errs2.into();
101 assert_eq!(s, "test");
102
103 let s: &str = (&errs).into();
104 assert_eq!(s, "test");
105
106 let s: &str = errs.as_str();
107 assert_eq!(s, "test");
108
109 let s = format!("{errs}");
110 assert_eq!(s, "test");
111
112 let s = format!("{errs:?}");
113 assert_eq!(s, "\"test\"");
114 }
115
116 #[test]
117 fn pciinfoerrorstring_string() {
118 let errs: PciInfoErrorString = "test".to_string().into();
119
120 let s: String = errs.to_string();
121 assert_eq!(s, "test");
122
123 let errs2 = errs.clone();
124 let s: String = errs2.into();
125 assert_eq!(s, "test");
126
127 let s: &str = (&errs).into();
128 assert_eq!(s, "test");
129
130 let s: &str = errs.as_str();
131 assert_eq!(s, "test");
132
133 let s = format!("{errs}");
134 assert_eq!(s, "test");
135
136 let s = format!("{errs:?}");
137 assert_eq!(s, "\"test\"");
138 }
139}