Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LuaPushable is not implemented for std::string::String #60

Closed
Avimitin opened this issue Sep 30, 2022 · 3 comments
Closed

LuaPushable is not implemented for std::string::String #60

Avimitin opened this issue Sep 30, 2022 · 3 comments

Comments

@Avimitin
Copy link

Sorry to bother you, again. The LuaPushable trait is not implemented for std::string::String, so Vec is still not a LuaPushable object. It seems like we need to implement this function. https://www.lua.org/manual/5.1/manual.html#lua_pushlstring

@Avimitin
Copy link
Author

Manage to make it work, but I don't know if it is correct to cast const *u8 to const *i8

diff --git a/crates/luajit-bindings/src/pushable.rs b/crates/luajit-bindings/src/pushable.rs
index 39876f2..3e68c92 100644
--- a/crates/luajit-bindings/src/pushable.rs
+++ b/crates/luajit-bindings/src/pushable.rs
@@ -127,6 +127,17 @@ impl<T: LuaPushable> LuaPushable for Vec<T> {
     }
 }
 
+impl LuaPushable for String {
+    unsafe fn push(
+            self,
+            lstate: *mut lua_State,
+        ) -> Result<c_int, crate::Error> {
+        ffi::lua_pushlstring(lstate, self.as_ptr() as _, self.len() as _);
+
+        Ok(1)
+    }
+}
+
 /// Implements `LuaPushable` for a tuple `(a, b, c, ..)` where all the elements
 /// in the tuple implement `LuaPushable`.
 macro_rules! push_tuple {

@Avimitin
Copy link
Author

It seems like Mlua crate is also using &[u8] for String

image

@noib3
Copy link
Owner

noib3 commented Oct 4, 2022

closed by 79130ba

@noib3 noib3 closed this as completed Oct 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants