1use std::borrow::Borrow;
6use std::hash::{Hash, Hasher};
7use std::ops::Deref;
8use std::os::raw::{c_int, c_void};
9use std::{cmp, fmt, mem, slice, str};
10
11use crate::error::{Error, Result};
12use crate::state::Lua;
13use crate::traits::IntoLua;
14use crate::types::{LuaType, ValueRef};
15use crate::value::Value;
16
17#[cfg(feature = "serde")]
18use {
19 serde::ser::{Serialize, Serializer},
20 std::result::Result as StdResult,
21};
22
23#[derive(Clone, PartialEq)]
27pub struct LuaString(pub(crate) ValueRef);
28
29impl LuaString {
30 #[inline]
52 pub fn to_str(&self) -> Result<BorrowedStr> {
53 BorrowedStr::try_from(self)
54 }
55
56 #[inline]
79 pub fn to_string_lossy(&self) -> String {
80 String::from_utf8_lossy(&self.as_bytes()).into_owned()
81 }
82
83 pub fn display(&self) -> impl fmt::Display + '_ {
90 Display(self)
91 }
92
93 #[inline]
112 pub fn as_bytes(&self) -> BorrowedBytes {
113 BorrowedBytes::from(self)
114 }
115
116 pub fn as_bytes_with_nul(&self) -> BorrowedBytes {
118 let BorrowedBytes { buf, vref, _lua } = BorrowedBytes::from(self);
119 let buf = unsafe { slice::from_raw_parts((*buf).as_ptr(), (*buf).len() + 1) };
121 BorrowedBytes { buf, vref, _lua }
122 }
123
124 unsafe fn to_slice(&self) -> (&[u8], Lua) {
126 let lua = self.0.lua.upgrade();
127 let slice = {
128 let rawlua = lua.lock();
129 let ref_thread = rawlua.ref_thread();
130
131 mlua_debug_assert!(
132 ffi::lua_type(ref_thread, self.0.index) == ffi::LUA_TSTRING,
133 "string ref is not string type"
134 );
135
136 let mut size = 0;
139 let data = ffi::lua_tolstring(ref_thread, self.0.index, &mut size);
140 slice::from_raw_parts(data as *const u8, size)
141 };
142 (slice, lua)
143 }
144
145 #[inline]
151 pub fn to_pointer(&self) -> *const c_void {
152 let lua = self.0.lua.lock();
155 unsafe { ffi::lua_tostring(lua.ref_thread(), self.0.index) as *const c_void }
156 }
157}
158
159impl fmt::Debug for LuaString {
160 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
161 let bytes = self.as_bytes();
162 if let Ok(s) = str::from_utf8(&bytes) {
164 return s.fmt(f);
165 }
166
167 write!(f, "b")?;
169 <bstr::BStr as fmt::Debug>::fmt(bstr::BStr::new(&bytes), f)
170 }
171}
172
173impl<T> PartialEq<T> for LuaString
181where
182 T: AsRef<[u8]> + ?Sized,
183{
184 fn eq(&self, other: &T) -> bool {
185 self.as_bytes() == other.as_ref()
186 }
187}
188
189impl Eq for LuaString {}
190
191impl<T> PartialOrd<T> for LuaString
192where
193 T: AsRef<[u8]> + ?Sized,
194{
195 fn partial_cmp(&self, other: &T) -> Option<cmp::Ordering> {
196 <[u8]>::partial_cmp(&self.as_bytes(), other.as_ref())
197 }
198}
199
200impl PartialOrd for LuaString {
201 fn partial_cmp(&self, other: &LuaString) -> Option<cmp::Ordering> {
202 Some(self.cmp(other))
203 }
204}
205
206impl Ord for LuaString {
207 fn cmp(&self, other: &LuaString) -> cmp::Ordering {
208 self.as_bytes().cmp(&other.as_bytes())
209 }
210}
211
212impl Hash for LuaString {
213 fn hash<H: Hasher>(&self, state: &mut H) {
214 self.as_bytes().hash(state);
215 }
216}
217
218#[cfg(feature = "serde")]
219impl Serialize for LuaString {
220 fn serialize<S>(&self, serializer: S) -> StdResult<S::Ok, S::Error>
221 where
222 S: Serializer,
223 {
224 match self.to_str() {
225 Ok(s) => serializer.serialize_str(&s),
226 Err(_) => serializer.serialize_bytes(&self.as_bytes()),
227 }
228 }
229}
230
231struct Display<'a>(&'a LuaString);
232
233impl fmt::Display for Display<'_> {
234 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
235 let bytes = self.0.as_bytes();
236 <bstr::BStr as fmt::Display>::fmt(bstr::BStr::new(&bytes), f)
237 }
238}
239
240pub struct BorrowedStr {
242 pub(crate) buf: &'static str,
244 pub(crate) vref: ValueRef,
245 pub(crate) _lua: Lua,
246}
247
248impl Deref for BorrowedStr {
249 type Target = str;
250
251 #[inline(always)]
252 fn deref(&self) -> &str {
253 self.buf
254 }
255}
256
257impl Borrow<str> for BorrowedStr {
258 #[inline(always)]
259 fn borrow(&self) -> &str {
260 self.buf
261 }
262}
263
264impl AsRef<str> for BorrowedStr {
265 #[inline(always)]
266 fn as_ref(&self) -> &str {
267 self.buf
268 }
269}
270
271impl fmt::Display for BorrowedStr {
272 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
273 self.buf.fmt(f)
274 }
275}
276
277impl fmt::Debug for BorrowedStr {
278 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
279 self.buf.fmt(f)
280 }
281}
282
283impl<T> PartialEq<T> for BorrowedStr
284where
285 T: AsRef<str>,
286{
287 fn eq(&self, other: &T) -> bool {
288 self.buf == other.as_ref()
289 }
290}
291
292impl Eq for BorrowedStr {}
293
294impl<T> PartialOrd<T> for BorrowedStr
295where
296 T: AsRef<str>,
297{
298 fn partial_cmp(&self, other: &T) -> Option<cmp::Ordering> {
299 self.buf.partial_cmp(other.as_ref())
300 }
301}
302
303impl Ord for BorrowedStr {
304 fn cmp(&self, other: &Self) -> cmp::Ordering {
305 self.buf.cmp(other.buf)
306 }
307}
308
309impl TryFrom<&LuaString> for BorrowedStr {
310 type Error = Error;
311
312 #[inline]
313 fn try_from(value: &LuaString) -> Result<Self> {
314 let BorrowedBytes { buf, vref, _lua } = BorrowedBytes::from(value);
315 let buf =
316 str::from_utf8(buf).map_err(|e| Error::from_lua_conversion("string", "&str", e.to_string()))?;
317 Ok(Self { buf, vref, _lua })
318 }
319}
320
321pub struct BorrowedBytes {
323 pub(crate) buf: &'static [u8],
325 pub(crate) vref: ValueRef,
326 pub(crate) _lua: Lua,
327}
328
329impl Deref for BorrowedBytes {
330 type Target = [u8];
331
332 #[inline(always)]
333 fn deref(&self) -> &[u8] {
334 self.buf
335 }
336}
337
338impl Borrow<[u8]> for BorrowedBytes {
339 #[inline(always)]
340 fn borrow(&self) -> &[u8] {
341 self.buf
342 }
343}
344
345impl AsRef<[u8]> for BorrowedBytes {
346 #[inline(always)]
347 fn as_ref(&self) -> &[u8] {
348 self.buf
349 }
350}
351
352impl fmt::Debug for BorrowedBytes {
353 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
354 self.buf.fmt(f)
355 }
356}
357
358impl<T> PartialEq<T> for BorrowedBytes
359where
360 T: AsRef<[u8]>,
361{
362 fn eq(&self, other: &T) -> bool {
363 self.buf == other.as_ref()
364 }
365}
366
367impl Eq for BorrowedBytes {}
368
369impl<T> PartialOrd<T> for BorrowedBytes
370where
371 T: AsRef<[u8]>,
372{
373 fn partial_cmp(&self, other: &T) -> Option<cmp::Ordering> {
374 self.buf.partial_cmp(other.as_ref())
375 }
376}
377
378impl Ord for BorrowedBytes {
379 fn cmp(&self, other: &Self) -> cmp::Ordering {
380 self.buf.cmp(other.buf)
381 }
382}
383
384impl<'a> IntoIterator for &'a BorrowedBytes {
385 type Item = &'a u8;
386 type IntoIter = slice::Iter<'a, u8>;
387
388 fn into_iter(self) -> Self::IntoIter {
389 self.iter()
390 }
391}
392
393impl From<&LuaString> for BorrowedBytes {
394 #[inline]
395 fn from(value: &LuaString) -> Self {
396 let (buf, _lua) = unsafe { value.to_slice() };
397 let vref = value.0.clone();
398 let buf = unsafe { mem::transmute::<&[u8], &'static [u8]>(buf) };
400 Self { buf, vref, _lua }
401 }
402}
403
404struct WrappedString<T: AsRef<[u8]>>(T);
405
406impl LuaString {
407 pub fn wrap(data: impl AsRef<[u8]>) -> impl IntoLua {
411 WrappedString(data)
412 }
413}
414
415impl<T: AsRef<[u8]>> IntoLua for WrappedString<T> {
416 fn into_lua(self, lua: &Lua) -> Result<Value> {
417 lua.create_string(self.0).map(Value::String)
418 }
419}
420
421impl LuaType for LuaString {
422 const TYPE_ID: c_int = ffi::LUA_TSTRING;
423}
424
425#[cfg(test)]
426mod assertions {
427 use super::*;
428
429 #[cfg(not(feature = "send"))]
430 static_assertions::assert_not_impl_any!(LuaString: Send);
431 #[cfg(feature = "send")]
432 static_assertions::assert_impl_all!(LuaString: Send, Sync);
433 #[cfg(feature = "send")]
434 static_assertions::assert_impl_all!(BorrowedBytes: Send, Sync);
435 #[cfg(feature = "send")]
436 static_assertions::assert_impl_all!(BorrowedStr: Send, Sync);
437}