irox_tools/
str.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// SPDX-License-Identifier: MIT
// Copyright 2025 IROX Contributors
//

extern crate alloc;
use alloc::string::{String, ToString};
use alloc::sync::Arc;
use irox_bits::{
    Bits, Error, MutBits, ReadFromBEBits, ReadFromLEBits, WriteToBEBits, WriteToLEBits,
};

///
/// Wrapper enum around the common string representations
#[derive(Clone, Eq, PartialEq, Debug, Hash)]
pub enum StrWrapper<'a> {
    Shared(Arc<String>),
    Owned(String),
    Borrowed(&'a str),
}
impl Default for StrWrapper<'_> {
    fn default() -> Self {
        StrWrapper::Owned(Default::default())
    }
}
impl AsRef<str> for StrWrapper<'_> {
    fn as_ref(&self) -> &str {
        self.as_str()
    }
}
impl AsMut<str> for StrWrapper<'_> {
    fn as_mut(&mut self) -> &mut str {
        match self {
            StrWrapper::Owned(o) => o,
            StrWrapper::Shared(s) => {
                *self = StrWrapper::Owned(s.to_string());
                self.as_mut()
            }
            StrWrapper::Borrowed(b) => {
                *self = StrWrapper::Owned((*b).to_string());
                self.as_mut()
            }
        }
    }
}
impl<'a> StrWrapper<'a> {
    #[must_use]
    pub fn as_str(&self) -> &str {
        match self {
            StrWrapper::Shared(s) => s.as_str(),
            StrWrapper::Owned(s) => s.as_str(),
            StrWrapper::Borrowed(s) => s,
        }
    }

    #[must_use]
    pub fn len(&self) -> usize {
        self.as_str().len()
    }

    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.as_str().is_empty()
    }

    #[must_use]
    pub fn to_owned(&self) -> Self {
        match self {
            StrWrapper::Shared(a) => a.as_ref().clone().into(),
            StrWrapper::Owned(o) => o.clone().into(),
            StrWrapper::Borrowed(s) => (*s).to_string().into(),
        }
    }

    #[must_use]
    pub fn to_shared(&self) -> Self {
        match self {
            StrWrapper::Shared(a) => a.clone().into(),
            StrWrapper::Owned(o) => Arc::new(o.clone()).into(),
            StrWrapper::Borrowed(s) => Arc::new((*s).to_string()).into(),
        }
    }

    pub fn make_shared(&mut self) {
        match self {
            StrWrapper::Shared(_) => {}
            StrWrapper::Owned(o) => {
                *self = StrWrapper::Shared(Arc::new(o.clone()));
            }
            StrWrapper::Borrowed(b) => {
                *self = StrWrapper::Shared(Arc::new((*b).to_string()));
            }
        }
    }
}
impl<'a> From<&'a str> for StrWrapper<'a> {
    fn from(s: &'a str) -> Self {
        StrWrapper::Borrowed(s)
    }
}
impl<'a> From<String> for StrWrapper<'a> {
    fn from(s: String) -> Self {
        StrWrapper::Owned(s)
    }
}
impl<'a> From<Arc<String>> for StrWrapper<'a> {
    fn from(s: Arc<String>) -> Self {
        StrWrapper::Shared(s)
    }
}
impl<'a> From<&Arc<String>> for StrWrapper<'a> {
    fn from(s: &Arc<String>) -> Self {
        StrWrapper::Shared(s.clone())
    }
}

impl<'a> WriteToBEBits for StrWrapper<'a> {
    fn write_be_to<T: MutBits + ?Sized>(&self, bits: &mut T) -> Result<usize, Error> {
        self.as_str().write_be_to(bits)
    }
}
impl<'a> ReadFromBEBits for StrWrapper<'a> {
    fn read_from_be_bits<T: Bits>(inp: &mut T) -> Result<Self, Error> {
        ReadFromBEBits::read_from_be_bits(inp).map(StrWrapper::Owned)
    }
}
impl<'a> WriteToLEBits for StrWrapper<'a> {
    fn write_le_to<T: MutBits + ?Sized>(&self, bits: &mut T) -> Result<usize, Error> {
        self.as_str().write_le_to(bits)
    }
}
impl<'a> ReadFromLEBits for StrWrapper<'a> {
    fn read_from_le_bits<T: Bits>(inp: &mut T) -> Result<Self, Error> {
        ReadFromLEBits::read_from_le_bits(inp).map(StrWrapper::Owned)
    }
}