pci_info/error/
pci_info_error_string.rs

1use std::{borrow::Cow, fmt::Debug, fmt::Display};
2
3// define an alias in case in the future we want
4// to use Rc or Box for some platforms.
5// Arc is best because it saves on cloning and is Send.
6type HeapPtr<T> = std::sync::Arc<T>;
7
8/// A string type used to optimize size and
9/// various operations on the error types of this crate.
10///
11/// # Examples
12/// This shows some conversions to `&str` and `String`
13/// using different methods:
14/// ```rust
15/// # use pci_info::PciInfoErrorString;
16/// let errs: PciInfoErrorString = "test".into();
17///
18/// // Convert to a String calling `to_string()`
19/// let s: String = errs.to_string();
20/// assert_eq!(s, "test");
21///
22/// // Convert to a String calling `into()`
23/// let errs2 = errs.clone();
24/// let s: String = errs2.into();
25/// assert_eq!(s, "test");
26///
27/// // Convert to a &str calling `into()` on a `&PciInfoErrorString`
28/// let s: &str = (&errs).into();
29/// assert_eq!(s, "test");
30///
31/// // Convert to a &str calling `as_str()`
32/// let s: &str = errs.as_str();
33/// assert_eq!(s, "test");
34///
35/// // Use the `Display` trait
36/// let s = format!("{errs}");
37/// assert_eq!(s, "test");
38///
39/// // Use the `Debug` trait
40/// let s = format!("{errs:?}");
41/// assert_eq!(s, "\"test\"");
42/// ```
43#[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}