mlua/string.rs
1use std::borrow::{Borrow, Cow};
2use std::hash::{Hash, Hasher};
3use std::os::raw::c_void;
4use std::string::String as StdString;
5use std::{fmt, slice, str};
6
7#[cfg(feature = "serialize")]
8use {
9 serde::ser::{Serialize, Serializer},
10 std::result::Result as StdResult,
11};
12
13use crate::error::{Error, Result};
14use crate::types::LuaRef;
15
16/// Handle to an internal Lua string.
17///
18/// Unlike Rust strings, Lua strings may not be valid UTF-8.
19#[derive(Clone)]
20pub struct String<'lua>(pub(crate) LuaRef<'lua>);
21
22/// Owned handle to an internal Lua string.
23///
24/// The owned handle holds a *strong* reference to the current Lua instance.
25/// Be warned, if you place it into a Lua type (eg. [`UserData`] or a Rust callback), it is *very easy*
26/// to accidentally cause reference cycles that would prevent destroying Lua instance.
27///
28/// [`UserData`]: crate::UserData
29#[cfg(feature = "unstable")]
30#[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
31#[derive(Clone)]
32pub struct OwnedString(pub(crate) crate::types::LuaOwnedRef);
33
34#[cfg(feature = "unstable")]
35impl OwnedString {
36 /// Get borrowed handle to the underlying Lua string.
37 #[cfg_attr(feature = "send", allow(unused))]
38 pub const fn to_ref(&self) -> String {
39 String(self.0.to_ref())
40 }
41}
42
43impl<'lua> String<'lua> {
44 /// Get a `&str` slice if the Lua string is valid UTF-8.
45 ///
46 /// # Examples
47 ///
48 /// ```
49 /// # use mlua::{Lua, Result, String};
50 /// # fn main() -> Result<()> {
51 /// # let lua = Lua::new();
52 /// let globals = lua.globals();
53 ///
54 /// let version: String = globals.get("_VERSION")?;
55 /// assert!(version.to_str()?.contains("Lua"));
56 ///
57 /// let non_utf8: String = lua.load(r#" "test\255" "#).eval()?;
58 /// assert!(non_utf8.to_str().is_err());
59 /// # Ok(())
60 /// # }
61 /// ```
62 #[inline]
63 pub fn to_str(&self) -> Result<&str> {
64 str::from_utf8(self.as_bytes()).map_err(|e| Error::FromLuaConversionError {
65 from: "string",
66 to: "&str",
67 message: Some(e.to_string()),
68 })
69 }
70
71 /// Converts this string to a [`Cow<str>`].
72 ///
73 /// Any non-Unicode sequences are replaced with [`U+FFFD REPLACEMENT CHARACTER`][U+FFFD].
74 ///
75 /// [U+FFFD]: std::char::REPLACEMENT_CHARACTER
76 ///
77 /// # Examples
78 ///
79 /// ```
80 /// # use mlua::{Lua, Result};
81 /// # fn main() -> Result<()> {
82 /// let lua = Lua::new();
83 ///
84 /// let s = lua.create_string(b"test\xff")?;
85 /// assert_eq!(s.to_string_lossy(), "test\u{fffd}");
86 /// # Ok(())
87 /// # }
88 /// ```
89 #[inline]
90 pub fn to_string_lossy(&self) -> Cow<'_, str> {
91 StdString::from_utf8_lossy(self.as_bytes())
92 }
93
94 /// Get the bytes that make up this string.
95 ///
96 /// The returned slice will not contain the terminating nul byte, but will contain any nul
97 /// bytes embedded into the Lua string.
98 ///
99 /// # Examples
100 ///
101 /// ```
102 /// # use mlua::{Lua, Result, String};
103 /// # fn main() -> Result<()> {
104 /// # let lua = Lua::new();
105 /// let non_utf8: String = lua.load(r#" "test\255" "#).eval()?;
106 /// assert!(non_utf8.to_str().is_err()); // oh no :(
107 /// assert_eq!(non_utf8.as_bytes(), &b"test\xff"[..]);
108 /// # Ok(())
109 /// # }
110 /// ```
111 #[inline]
112 pub fn as_bytes(&self) -> &[u8] {
113 let nulled = self.as_bytes_with_nul();
114 &nulled[..nulled.len() - 1]
115 }
116
117 /// Get the bytes that make up this string, including the trailing nul byte.
118 pub fn as_bytes_with_nul(&self) -> &[u8] {
119 let ref_thread = self.0.lua.ref_thread();
120 unsafe {
121 mlua_debug_assert!(
122 ffi::lua_type(ref_thread, self.0.index) == ffi::LUA_TSTRING,
123 "string ref is not string type"
124 );
125
126 let mut size = 0;
127 // This will not trigger a 'm' error, because the reference is guaranteed to be of
128 // string type
129 let data = ffi::lua_tolstring(ref_thread, self.0.index, &mut size);
130
131 slice::from_raw_parts(data as *const u8, size + 1)
132 }
133 }
134
135 /// Converts this string to a generic C pointer.
136 ///
137 /// There is no way to convert the pointer back to its original value.
138 ///
139 /// Typically this function is used only for hashing and debug information.
140 #[inline]
141 pub fn to_pointer(&self) -> *const c_void {
142 self.0.to_pointer()
143 }
144
145 /// Convert this handle to owned version.
146 #[cfg(all(feature = "unstable", any(not(feature = "send"), doc)))]
147 #[cfg_attr(docsrs, doc(cfg(all(feature = "unstable", not(feature = "send")))))]
148 #[inline]
149 pub fn into_owned(self) -> OwnedString {
150 OwnedString(self.0.into_owned())
151 }
152}
153
154impl<'lua> fmt::Debug for String<'lua> {
155 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
156 let bytes = self.as_bytes();
157 // Check if the string is valid utf8
158 if let Ok(s) = str::from_utf8(bytes) {
159 return s.fmt(f);
160 }
161
162 // Format as bytes
163 write!(f, "b\"")?;
164 for &b in bytes {
165 // https://doc.rust-lang.org/reference/tokens.html#byte-escapes
166 match b {
167 b'\n' => write!(f, "\\n")?,
168 b'\r' => write!(f, "\\r")?,
169 b'\t' => write!(f, "\\t")?,
170 b'\\' | b'"' => write!(f, "\\{}", b as char)?,
171 b'\0' => write!(f, "\\0")?,
172 // ASCII printable
173 0x20..=0x7e => write!(f, "{}", b as char)?,
174 _ => write!(f, "\\x{b:02x}")?,
175 }
176 }
177 write!(f, "\"")?;
178
179 Ok(())
180 }
181}
182
183impl<'lua> AsRef<[u8]> for String<'lua> {
184 fn as_ref(&self) -> &[u8] {
185 self.as_bytes()
186 }
187}
188
189impl<'lua> Borrow<[u8]> for String<'lua> {
190 fn borrow(&self) -> &[u8] {
191 self.as_bytes()
192 }
193}
194
195// Lua strings are basically &[u8] slices, so implement PartialEq for anything resembling that.
196//
197// This makes our `String` comparable with `Vec<u8>`, `[u8]`, `&str`, `String` and `mlua::String`
198// itself.
199//
200// The only downside is that this disallows a comparison with `Cow<str>`, as that only implements
201// `AsRef<str>`, which collides with this impl. Requiring `AsRef<str>` would fix that, but limit us
202// in other ways.
203impl<'lua, T> PartialEq<T> for String<'lua>
204where
205 T: AsRef<[u8]> + ?Sized,
206{
207 fn eq(&self, other: &T) -> bool {
208 self.as_bytes() == other.as_ref()
209 }
210}
211
212impl<'lua> Eq for String<'lua> {}
213
214impl<'lua> Hash for String<'lua> {
215 fn hash<H: Hasher>(&self, state: &mut H) {
216 self.as_bytes().hash(state);
217 }
218}
219
220#[cfg(feature = "serialize")]
221impl<'lua> Serialize for String<'lua> {
222 fn serialize<S>(&self, serializer: S) -> StdResult<S::Ok, S::Error>
223 where
224 S: Serializer,
225 {
226 match self.to_str() {
227 Ok(s) => serializer.serialize_str(s),
228 Err(_) => serializer.serialize_bytes(self.as_bytes()),
229 }
230 }
231}
232
233// Additional shortcuts
234#[cfg(feature = "unstable")]
235impl OwnedString {
236 /// Get a `&str` slice if the Lua string is valid UTF-8.
237 ///
238 /// This is a shortcut for [`String::to_str()`].
239 #[inline]
240 pub fn to_str(&self) -> Result<&str> {
241 let s = self.to_ref();
242 // Reattach lifetime to &self
243 unsafe { std::mem::transmute(s.to_str()) }
244 }
245
246 /// Get the bytes that make up this string.
247 ///
248 /// This is a shortcut for [`String::as_bytes()`].
249 #[inline]
250 pub fn as_bytes(&self) -> &[u8] {
251 let s = self.to_ref();
252 // Reattach lifetime to &self
253 unsafe { std::mem::transmute(s.as_bytes()) }
254 }
255}
256
257#[cfg(feature = "unstable")]
258impl fmt::Debug for OwnedString {
259 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
260 self.to_ref().fmt(f)
261 }
262}
263
264#[cfg(test)]
265mod assertions {
266 use super::*;
267
268 static_assertions::assert_not_impl_any!(String: Send);
269}