Skip to content

Commit a3302af

Browse files
committed
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
1 parent dfb4e9a commit a3302af

File tree

5 files changed

+21
-22
lines changed

5 files changed

+21
-22
lines changed

src/string.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ impl String {
8686

8787
/// Get the bytes that make up this string.
8888
///
89-
/// The returned slice will not contain the terminating nul byte, but will contain any nul
89+
/// The returned slice will not contain the terminating null byte, but will contain any null
9090
/// bytes embedded into the Lua string.
9191
///
9292
/// # Examples
@@ -106,15 +106,15 @@ impl String {
106106
BorrowedBytes::from(self)
107107
}
108108

109-
/// Get the bytes that make up this string, including the trailing nul byte.
109+
/// Get the bytes that make up this string, including the trailing null byte.
110110
pub fn as_bytes_with_nul(&self) -> BorrowedBytes<'_> {
111111
let BorrowedBytes { buf, borrow, _lua } = BorrowedBytes::from(self);
112-
// Include the trailing nul byte (it's always present but excluded by default)
112+
// Include the trailing null byte (it's always present but excluded by default)
113113
let buf = unsafe { slice::from_raw_parts((*buf).as_ptr(), (*buf).len() + 1) };
114114
BorrowedBytes { buf, borrow, _lua }
115115
}
116116

117-
// Does not return the terminating nul byte
117+
// Does not return the terminating null byte
118118
unsafe fn to_slice(&self) -> (&[u8], Lua) {
119119
let lua = self.0.lua.upgrade();
120120
let slice = {

src/value.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,10 @@ impl Value {
356356
///
357357
/// If the value is a Lua [`String`], try to convert it to [`BorrowedStr`] or return `None`
358358
/// otherwise.
359+
#[deprecated(
360+
since = "0.11.0",
361+
note = "This method does not follow Rust naming convention. Use `as_string().and_then(|s| s.to_str().ok())` instead."
362+
)]
359363
#[inline]
360364
pub fn as_str(&self) -> Option<BorrowedStr<'_>> {
361365
self.as_string().and_then(|s| s.to_str().ok())
@@ -364,6 +368,10 @@ impl Value {
364368
/// Cast the value to [`StdString`].
365369
///
366370
/// If the value is a Lua [`String`], converts it to [`StdString`] or returns `None` otherwise.
371+
#[deprecated(
372+
since = "0.11.0",
373+
note = "This method does not follow Rust naming convention. Use `as_string().map(|s| s.to_string_lossy())` instead."
374+
)]
367375
#[inline]
368376
pub fn as_string_lossy(&self) -> Option<StdString> {
369377
self.as_string().map(|s| s.to_string_lossy())

tests/conversion.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ fn test_registry_value_into_lua() -> Result<()> {
267267
let r = lua.create_registry_value(&s)?;
268268
let value1 = lua.pack(&r)?;
269269
let value2 = lua.pack(r)?;
270-
assert_eq!(value1.as_str().as_deref(), Some("hello, world"));
270+
assert_eq!(value1.to_string()?, "hello, world");
271271
assert_eq!(value1.to_pointer(), value2.to_pointer());
272272

273273
// Push into stack
@@ -560,11 +560,11 @@ fn test_osstring_into_from_lua() -> Result<()> {
560560

561561
let v = lua.pack(s.as_os_str())?;
562562
assert!(v.is_string());
563-
assert_eq!(v.as_str().unwrap(), "hello, world");
563+
assert_eq!(v.as_string().unwrap(), "hello, world");
564564

565565
let v = lua.pack(s)?;
566566
assert!(v.is_string());
567-
assert_eq!(v.as_str().unwrap(), "hello, world");
567+
assert_eq!(v.as_string().unwrap(), "hello, world");
568568

569569
let s = lua.create_string("hello, world")?;
570570
let bstr = lua.unpack::<OsString>(Value::String(s))?;
@@ -588,11 +588,11 @@ fn test_pathbuf_into_from_lua() -> Result<()> {
588588

589589
let v = lua.pack(pb.as_path())?;
590590
assert!(v.is_string());
591-
assert_eq!(v.as_str().unwrap(), pb_str);
591+
assert_eq!(v.to_string().unwrap(), pb_str);
592592

593593
let v = lua.pack(pb.clone())?;
594594
assert!(v.is_string());
595-
assert_eq!(v.as_str().unwrap(), pb_str);
595+
assert_eq!(v.to_string().unwrap(), pb_str);
596596

597597
let s = lua.create_string(pb_str)?;
598598
let bstr = lua.unpack::<PathBuf>(Value::String(s))?;
@@ -724,7 +724,7 @@ fn test_char_into_lua() -> Result<()> {
724724

725725
let v = '🦀';
726726
let v2 = v.into_lua(&lua)?;
727-
assert_eq!(Some(v.to_string()), v2.as_string_lossy());
727+
assert_eq!(*v2.as_string().unwrap(), v.to_string());
728728

729729
Ok(())
730730
}

tests/multi.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@ fn test_result_conversions() -> Result<()> {
4343
let multi_err1 = err1.into_lua_multi(&lua)?;
4444
assert_eq!(multi_err1.len(), 2);
4545
assert_eq!(multi_err1[0], Value::Nil);
46-
assert_eq!(multi_err1[1].as_str().unwrap(), "failure1");
46+
assert_eq!(multi_err1[1].as_string().unwrap(), "failure1");
4747

4848
let ok2 = Ok::<_, Error>("!");
4949
let multi_ok2 = ok2.into_lua_multi(&lua)?;
5050
assert_eq!(multi_ok2.len(), 1);
51-
assert_eq!(multi_ok2[0].as_str().unwrap(), "!");
51+
assert_eq!(multi_ok2[0].as_string().unwrap(), "!");
5252
let err2 = Err::<String, _>("failure2".into_lua_err());
5353
let multi_err2 = err2.into_lua_multi(&lua)?;
5454
assert_eq!(multi_err2.len(), 2);

tests/value.rs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -255,16 +255,7 @@ fn test_value_conversions() -> Result<()> {
255255
Value::String(lua.create_string("hello")?).as_string().unwrap(),
256256
"hello"
257257
);
258-
assert_eq!(
259-
Value::String(lua.create_string("hello")?).as_str().unwrap(),
260-
"hello"
261-
);
262-
assert_eq!(
263-
Value::String(lua.create_string("hello")?)
264-
.as_string_lossy()
265-
.unwrap(),
266-
"hello"
267-
);
258+
assert_eq!(Value::String(lua.create_string("hello")?).to_string()?, "hello");
268259
assert!(Value::Table(lua.create_table()?).is_table());
269260
assert!(Value::Table(lua.create_table()?).as_table().is_some());
270261
assert!(Value::Function(lua.create_function(|_, ()| Ok(())).unwrap()).is_function());

0 commit comments

Comments
 (0)