Skip to content

Commit 6a06e4d

Browse files
author
Egor Ivkov
committed
feat: impl Push and LuaRead for Path and OsStr related types
1 parent 09f7136 commit 6a06e4d

File tree

4 files changed

+81
-4
lines changed

4 files changed

+81
-4
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
# Change Log
22

33

4-
# [?.?.?] Unreleased
4+
# [5.1.0] Unreleased
55

66
### Added
7+
- `tlua::Push` trait implementations for `OsString`, `OsStr`, `Path`, `PathBuf`
8+
- `tlua::LuaRead` trait implementations for `OsString`, `PathBuf`
79

810
### Changed
911

tarantool/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "tarantool"
33
description = "Tarantool rust bindings"
4-
version = "5.0.1"
4+
version = "5.1.0"
55
authors = [
66
"Dmitriy Koltsov <dkoltsov@picodata.io>",
77
"Georgy Moshkin <gmoshkin@picodata.io>",

tests/src/tlua/values.rs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
use std::ffi::CString;
1+
use std::ffi::{CString, OsStr, OsString};
22
use std::os::raw::{c_char, c_void};
3+
use std::path::{Path, PathBuf};
34
use tarantool::tlua::{
45
c_ptr, c_str, ffi, function0, AnyLuaString, AnyLuaValue, AsCData, AsLua, AsTable, CData,
56
CDataOnStack, False, Lua, LuaFunction, LuaTable, Nil, Null, Strict, StringInLua, ToString,
@@ -883,13 +884,34 @@ pub fn readwrite_bools() {
883884
}
884885

885886
pub fn readwrite_strings() {
887+
use tarantool::tlua;
888+
886889
let lua = Lua::new();
887890

891+
#[derive(tlua::Push)]
892+
struct S<'a, 'b> {
893+
os: &'a OsStr,
894+
path: &'b Path,
895+
}
896+
897+
#[derive(tlua::Push, tlua::LuaRead)]
898+
struct OwnedS {
899+
os: OsString,
900+
path: PathBuf,
901+
}
902+
888903
lua.set("a", "hello");
889904
lua.set("b", &"hello".to_string());
890905
lua.set("c", c_str!("can you hear me?"));
891906
lua.set("d", CString::new("HELLO!!!").unwrap());
892907

908+
lua.checked_set("os_owned", OsString::from("OsString"))
909+
.unwrap();
910+
lua.checked_set("os", &OsStr::new("OsStr")).unwrap();
911+
lua.checked_set("path_owned", PathBuf::from("./path/owned"))
912+
.unwrap();
913+
lua.checked_set("path", Path::new("./path")).unwrap();
914+
893915
let x: String = lua.get("a").unwrap();
894916
assert_eq!(x, "hello");
895917

@@ -899,6 +921,17 @@ pub fn readwrite_strings() {
899921
assert_eq!(lua.get::<String, _>("c").unwrap(), "can you hear me?");
900922
assert_eq!(lua.get::<String, _>("d").unwrap(), "HELLO!!!");
901923

924+
assert_eq!(lua.get::<OsString, _>("os_owned").unwrap(), "OsString");
925+
assert_eq!(lua.get::<OsString, _>("os").unwrap(), "OsStr");
926+
assert_eq!(
927+
lua.get::<PathBuf, _>("path_owned").unwrap(),
928+
Path::new(".").join("path").join("owned")
929+
);
930+
assert_eq!(
931+
lua.get::<PathBuf, _>("path").unwrap(),
932+
Path::new(".").join("path")
933+
);
934+
902935
assert_eq!(lua.eval::<String>("return 'abc'").unwrap(), "abc");
903936
assert_eq!(
904937
lua.eval::<CString>("return 'abc'").unwrap().as_ref(),

tlua/src/values.rs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
use std::borrow::Cow;
22
use std::convert::TryFrom;
3-
use std::ffi::{CStr, CString};
3+
use std::ffi::{CStr, CString, OsStr, OsString};
44
use std::mem::MaybeUninit;
55
use std::num::NonZeroI32;
66
use std::ops::Deref;
77
use std::os::raw::{c_int, c_void};
8+
use std::os::unix::ffi::{OsStrExt, OsStringExt};
9+
use std::path::{Path, PathBuf};
810
use std::ptr::null_mut;
911
use std::slice;
1012
use std::str;
@@ -419,6 +421,46 @@ impl_push_read! { CStr,
419421
}
420422
}
421423

424+
impl_push_read! { OsString,
425+
push_to_lua(&self, lua) {
426+
push_string_impl!(self, lua)
427+
}
428+
push_into_lua(self, lua) {
429+
push_string_impl!(self, lua)
430+
}
431+
read_at_position(lua, index) {
432+
lua_read_string_impl!(lua, index,
433+
|slice: &[u8], _| Ok(OsString::from_vec(slice.to_vec()))
434+
)
435+
}
436+
}
437+
438+
impl_push_read! { OsStr,
439+
push_to_lua(&self, lua) {
440+
push_string_impl!(self, lua)
441+
}
442+
}
443+
444+
impl_push_read! { PathBuf,
445+
push_to_lua(&self, lua) {
446+
self.as_path().push_to_lua(lua)
447+
}
448+
push_into_lua(self, lua) {
449+
let s = self.into_os_string();
450+
push_string_impl!(s, lua)
451+
}
452+
read_at_position(lua, index) {
453+
OsString::lua_read_at_position(lua, index).map(Into::into)
454+
}
455+
}
456+
457+
impl_push_read! { Path,
458+
push_to_lua(&self, lua) {
459+
let s = self.as_os_str();
460+
push_string_impl!(s, lua)
461+
}
462+
}
463+
422464
/// String on the Lua stack.
423465
///
424466
/// It is faster -but less convenient- to read a `StringInLua` rather than a `String` because you

0 commit comments

Comments
 (0)