Rename string::String to LuaString

This commit is contained in:
Alex Orlenko
2026-01-29 18:45:41 +00:00
parent c1ffd4e790
commit 2ace892613
26 changed files with 246 additions and 264 deletions
+14 -15
View File
@@ -1,14 +1,13 @@
use std::cmp::Ordering;
use std::collections::HashSet;
use std::os::raw::c_void;
use std::string::String as StdString;
use std::{fmt, ptr, str};
use num_traits::FromPrimitive;
use crate::error::{Error, Result};
use crate::function::Function;
use crate::string::{BorrowedStr, String};
use crate::string::{BorrowedStr, LuaString};
use crate::table::Table;
use crate::thread::Thread;
use crate::types::{Integer, LightUserData, Number, ValueRef};
@@ -50,7 +49,7 @@ pub enum Value {
/// An interned string, managed by Lua.
///
/// Unlike Rust strings, Lua strings may not be valid UTF-8.
String(String),
String(LuaString),
/// Reference to a Lua table.
Table(Table),
/// Reference to a Lua function (or closure).
@@ -129,7 +128,7 @@ impl Value {
#[inline]
pub fn to_pointer(&self) -> *const c_void {
match self {
Value::String(String(vref)) => {
Value::String(LuaString(vref)) => {
// In Lua < 5.4 (excluding Luau), string pointers are NULL
// Use alternative approach
let lua = vref.lua.lock();
@@ -151,8 +150,8 @@ impl Value {
///
/// This might invoke the `__tostring` metamethod for non-primitive types (eg. tables,
/// functions).
pub fn to_string(&self) -> Result<StdString> {
unsafe fn invoke_to_string(vref: &ValueRef) -> Result<StdString> {
pub fn to_string(&self) -> Result<String> {
unsafe fn invoke_to_string(vref: &ValueRef) -> Result<String> {
let lua = vref.lua.lock();
let state = lua.state();
let _guard = StackGuard::new(state);
@@ -162,7 +161,7 @@ impl Value {
protect_lua!(state, 1, 1, fn(state) {
ffi::luaL_tolstring(state, -1, ptr::null_mut());
})?;
Ok(String(lua.pop_ref()).to_str()?.to_string())
Ok(LuaString(lua.pop_ref()).to_str()?.to_string())
}
match self {
@@ -336,17 +335,17 @@ impl Value {
self.as_number()
}
/// Returns `true` if the value is a Lua [`String`].
/// Returns `true` if the value is a [`LuaString`].
#[inline]
pub fn is_string(&self) -> bool {
self.as_string().is_some()
}
/// Cast the value to Lua [`String`].
/// Cast the value to a [`LuaString`].
///
/// If the value is a Lua [`String`], returns it or `None` otherwise.
/// If the value is a [`LuaString`], returns it or `None` otherwise.
#[inline]
pub fn as_string(&self) -> Option<&String> {
pub fn as_string(&self) -> Option<&LuaString> {
match self {
Value::String(s) => Some(s),
_ => None,
@@ -355,7 +354,7 @@ impl Value {
/// Cast the value to [`BorrowedStr`].
///
/// If the value is a Lua [`String`], try to convert it to [`BorrowedStr`] or return `None`
/// If the value is a [`LuaString`], try to convert it to [`BorrowedStr`] or return `None`
/// otherwise.
#[deprecated(
since = "0.11.0",
@@ -366,15 +365,15 @@ impl Value {
self.as_string().and_then(|s| s.to_str().ok())
}
/// Cast the value to [`StdString`].
/// Cast the value to [`String`].
///
/// If the value is a Lua [`String`], converts it to [`StdString`] or returns `None` otherwise.
/// If the value is a [`LuaString`], converts it to [`String`] or returns `None` otherwise.
#[deprecated(
since = "0.11.0",
note = "This method does not follow Rust naming convention. Use `as_string().map(|s| s.to_string_lossy())` instead."
)]
#[inline]
pub fn as_string_lossy(&self) -> Option<StdString> {
pub fn as_string_lossy(&self) -> Option<String> {
self.as_string().map(|s| s.to_string_lossy())
}