mirror of
https://github.com/mlua-rs/mlua
synced 2026-06-08 16:05:43 +00:00
Deprecate Value::as_str and Value::as_string_lossy
These methods don't follow Rust naming convention, see https://rust-lang.github.io/api-guidelines/naming.html#ad-hoc-conversions-follow-as_-to_-into_-conventions-c-conv
This commit is contained in:
+4
-4
@@ -86,7 +86,7 @@ impl String {
|
||||
|
||||
/// Get the bytes that make up this string.
|
||||
///
|
||||
/// The returned slice will not contain the terminating nul byte, but will contain any nul
|
||||
/// The returned slice will not contain the terminating null byte, but will contain any null
|
||||
/// bytes embedded into the Lua string.
|
||||
///
|
||||
/// # Examples
|
||||
@@ -106,15 +106,15 @@ impl String {
|
||||
BorrowedBytes::from(self)
|
||||
}
|
||||
|
||||
/// Get the bytes that make up this string, including the trailing nul byte.
|
||||
/// Get the bytes that make up this string, including the trailing null byte.
|
||||
pub fn as_bytes_with_nul(&self) -> BorrowedBytes<'_> {
|
||||
let BorrowedBytes { buf, borrow, _lua } = BorrowedBytes::from(self);
|
||||
// Include the trailing nul byte (it's always present but excluded by default)
|
||||
// Include the trailing null byte (it's always present but excluded by default)
|
||||
let buf = unsafe { slice::from_raw_parts((*buf).as_ptr(), (*buf).len() + 1) };
|
||||
BorrowedBytes { buf, borrow, _lua }
|
||||
}
|
||||
|
||||
// Does not return the terminating nul byte
|
||||
// Does not return the terminating null byte
|
||||
unsafe fn to_slice(&self) -> (&[u8], Lua) {
|
||||
let lua = self.0.lua.upgrade();
|
||||
let slice = {
|
||||
|
||||
@@ -356,6 +356,10 @@ impl Value {
|
||||
///
|
||||
/// If the value is a Lua [`String`], try to convert it to [`BorrowedStr`] or return `None`
|
||||
/// otherwise.
|
||||
#[deprecated(
|
||||
since = "0.11.0",
|
||||
note = "This method does not follow Rust naming convention. Use `as_string().and_then(|s| s.to_str().ok())` instead."
|
||||
)]
|
||||
#[inline]
|
||||
pub fn as_str(&self) -> Option<BorrowedStr<'_>> {
|
||||
self.as_string().and_then(|s| s.to_str().ok())
|
||||
@@ -364,6 +368,10 @@ impl Value {
|
||||
/// Cast the value to [`StdString`].
|
||||
///
|
||||
/// If the value is a Lua [`String`], converts it to [`StdString`] 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> {
|
||||
self.as_string().map(|s| s.to_string_lossy())
|
||||
|
||||
+6
-6
@@ -267,7 +267,7 @@ fn test_registry_value_into_lua() -> Result<()> {
|
||||
let r = lua.create_registry_value(&s)?;
|
||||
let value1 = lua.pack(&r)?;
|
||||
let value2 = lua.pack(r)?;
|
||||
assert_eq!(value1.as_str().as_deref(), Some("hello, world"));
|
||||
assert_eq!(value1.to_string()?, "hello, world");
|
||||
assert_eq!(value1.to_pointer(), value2.to_pointer());
|
||||
|
||||
// Push into stack
|
||||
@@ -560,11 +560,11 @@ fn test_osstring_into_from_lua() -> Result<()> {
|
||||
|
||||
let v = lua.pack(s.as_os_str())?;
|
||||
assert!(v.is_string());
|
||||
assert_eq!(v.as_str().unwrap(), "hello, world");
|
||||
assert_eq!(v.as_string().unwrap(), "hello, world");
|
||||
|
||||
let v = lua.pack(s)?;
|
||||
assert!(v.is_string());
|
||||
assert_eq!(v.as_str().unwrap(), "hello, world");
|
||||
assert_eq!(v.as_string().unwrap(), "hello, world");
|
||||
|
||||
let s = lua.create_string("hello, world")?;
|
||||
let bstr = lua.unpack::<OsString>(Value::String(s))?;
|
||||
@@ -588,11 +588,11 @@ fn test_pathbuf_into_from_lua() -> Result<()> {
|
||||
|
||||
let v = lua.pack(pb.as_path())?;
|
||||
assert!(v.is_string());
|
||||
assert_eq!(v.as_str().unwrap(), pb_str);
|
||||
assert_eq!(v.to_string().unwrap(), pb_str);
|
||||
|
||||
let v = lua.pack(pb.clone())?;
|
||||
assert!(v.is_string());
|
||||
assert_eq!(v.as_str().unwrap(), pb_str);
|
||||
assert_eq!(v.to_string().unwrap(), pb_str);
|
||||
|
||||
let s = lua.create_string(pb_str)?;
|
||||
let bstr = lua.unpack::<PathBuf>(Value::String(s))?;
|
||||
@@ -724,7 +724,7 @@ fn test_char_into_lua() -> Result<()> {
|
||||
|
||||
let v = '🦀';
|
||||
let v2 = v.into_lua(&lua)?;
|
||||
assert_eq!(Some(v.to_string()), v2.as_string_lossy());
|
||||
assert_eq!(*v2.as_string().unwrap(), v.to_string());
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
+2
-2
@@ -43,12 +43,12 @@ fn test_result_conversions() -> Result<()> {
|
||||
let multi_err1 = err1.into_lua_multi(&lua)?;
|
||||
assert_eq!(multi_err1.len(), 2);
|
||||
assert_eq!(multi_err1[0], Value::Nil);
|
||||
assert_eq!(multi_err1[1].as_str().unwrap(), "failure1");
|
||||
assert_eq!(multi_err1[1].as_string().unwrap(), "failure1");
|
||||
|
||||
let ok2 = Ok::<_, Error>("!");
|
||||
let multi_ok2 = ok2.into_lua_multi(&lua)?;
|
||||
assert_eq!(multi_ok2.len(), 1);
|
||||
assert_eq!(multi_ok2[0].as_str().unwrap(), "!");
|
||||
assert_eq!(multi_ok2[0].as_string().unwrap(), "!");
|
||||
let err2 = Err::<String, _>("failure2".into_lua_err());
|
||||
let multi_err2 = err2.into_lua_multi(&lua)?;
|
||||
assert_eq!(multi_err2.len(), 2);
|
||||
|
||||
+1
-10
@@ -255,16 +255,7 @@ fn test_value_conversions() -> Result<()> {
|
||||
Value::String(lua.create_string("hello")?).as_string().unwrap(),
|
||||
"hello"
|
||||
);
|
||||
assert_eq!(
|
||||
Value::String(lua.create_string("hello")?).as_str().unwrap(),
|
||||
"hello"
|
||||
);
|
||||
assert_eq!(
|
||||
Value::String(lua.create_string("hello")?)
|
||||
.as_string_lossy()
|
||||
.unwrap(),
|
||||
"hello"
|
||||
);
|
||||
assert_eq!(Value::String(lua.create_string("hello")?).to_string()?, "hello");
|
||||
assert!(Value::Table(lua.create_table()?).is_table());
|
||||
assert!(Value::Table(lua.create_table()?).as_table().is_some());
|
||||
assert!(Value::Function(lua.create_function(|_, ()| Ok(())).unwrap()).is_function());
|
||||
|
||||
Reference in New Issue
Block a user