diff --git a/src/string.rs b/src/string.rs index df9bb81..eeb08ed 100644 --- a/src/string.rs +++ b/src/string.rs @@ -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 = { diff --git a/src/value.rs b/src/value.rs index 2edef35..662ea6a 100644 --- a/src/value.rs +++ b/src/value.rs @@ -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> { 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 { self.as_string().map(|s| s.to_string_lossy()) diff --git a/tests/conversion.rs b/tests/conversion.rs index d724fa8..7322853 100644 --- a/tests/conversion.rs +++ b/tests/conversion.rs @@ -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::(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::(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(()) } diff --git a/tests/multi.rs b/tests/multi.rs index 13ce687..3208555 100644 --- a/tests/multi.rs +++ b/tests/multi.rs @@ -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::("failure2".into_lua_err()); let multi_err2 = err2.into_lua_multi(&lua)?; assert_eq!(multi_err2.len(), 2); diff --git a/tests/value.rs b/tests/value.rs index 4185442..0ff4844 100644 --- a/tests/value.rs +++ b/tests/value.rs @@ -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());