1#![allow(clippy::should_implement_trait)]
24#![allow(clippy::from_over_into)]
25
26use crate::{WideChar, RawWithNul, shims::ncurses::wchar_t};
27
28#[derive(Clone, Debug, PartialEq, Eq, Hash)]
30pub struct WideString {
31 inner: Vec<wchar_t>
32}
33
34impl WideString {
35 pub fn new() -> Self {
36 Self { inner: vec!() }
37 }
38
39 #[deprecated(since = "0.6.4", note = "use `From` trait instead!")]
40 pub fn from_str<S: Into<String>>(str: S) -> Self {
41 Self { inner: str.into().chars().map(|c| u32::from(c) as wchar_t).collect() }
42 }
43
44 pub fn with_capacity(capacity: usize) -> Self {
45 Self { inner: Vec::with_capacity(capacity) }
46 }
47
48 pub fn push_str(&mut self, rhs: &Self) {
53 self.inner.append(&mut Self::into(rhs.to_owned()));
54 }
55
56 pub fn capacity(&self) -> usize {
57 self.inner.capacity()
58 }
59
60 pub fn reserve(&mut self, additional: usize) {
61 self.inner.reserve(additional)
62 }
63
64 pub fn reserve_exact(&mut self, additional: usize) {
65 self.inner.reserve_exact(additional)
66 }
67
68 pub fn shrink_to_fit(&mut self) {
69 self.inner.shrink_to_fit()
70 }
71
72 pub fn push(&mut self, rhs: WideChar) {
73 self.inner.push(WideChar::into(rhs.to_owned()));
74 }
75
76 pub fn truncate(&mut self, new_len: usize) {
77 self.inner.truncate(new_len)
78 }
79
80 pub fn pop(&mut self) -> Option<WideChar> {
81 self.inner.pop().map(WideChar::from)
82 }
83
84 pub fn remove(&mut self, idx: usize) -> WideChar {
85 WideChar::from(self.inner.remove(idx))
86 }
87
88 pub fn insert(&mut self, idx: usize, ch: WideChar) {
89 self.inner.insert(idx, WideChar::into(ch))
90 }
91
92 pub fn len(&self) -> usize {
93 self.inner.len()
94 }
95
96 pub fn is_empty(&self) -> bool {
97 self.len() == 0
98 }
99
100 pub fn clear(&mut self) {
101 self.inner.clear()
102 }
103}
104
105impl Default for WideString {
106 fn default() -> Self {
107 Self::new()
108 }
109}
110
111impl <'a>From<&'a Vec<WideChar>> for WideString {
112 fn from(vwch: &'a Vec<WideChar>) -> Self {
113 Self { inner: vwch.iter().map(|wch| WideChar::into(*wch)).collect() }
114 }
115}
116
117impl <'a>From<&'a [wchar_t]> for WideString {
118 fn from(slice: &'a [wchar_t]) -> Self {
119 Self { inner : slice.to_vec() }
120 }
121}
122
123impl Into<Vec<wchar_t>> for WideString {
124 fn into(self) -> Vec<wchar_t> {
125 self.inner
126 }
127}
128
129impl From<&str> for WideString {
130 fn from(value: &str) -> Self {
131 Self { inner: value.chars().map(|chr| u32::from(chr) as wchar_t).collect() }
132 }
133}
134
135impl From<String> for WideString {
136 fn from(value: String) -> Self {
137 Self::from(value.as_str())
138 }
139}
140
141impl RawWithNul<Vec<wchar_t>> for WideString {
142 fn raw_with_nul(self) -> Vec<wchar_t> {
143 let mut raw: Vec<wchar_t> = Self::into(self);
144
145 raw.push(0x00);
146
147 raw
148 }
149}
150
151impl AsRef<WideString> for WideString {
152 fn as_ref(&self) -> &Self {
153 self
154 }
155}
156
157impl AsMut<WideString> for WideString {
158 fn as_mut(&mut self) -> &mut Self {
159 self
160 }
161}