Skip to content

Commit 5ccba98

Browse files
committed
Add bytes module
1 parent f1a4f9b commit 5ccba98

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

src/bytes.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ use std::ops::{Deref, DerefMut};
22

33
use bytes::Bytes;
44
use mlua::{
5-
BorrowedBytes, Error, FromLua, Lua, MetaMethod, Result, String as LuaString, UserData, UserDataMethods,
6-
UserDataRegistry, Value,
5+
BorrowedBytes, Error, FromLua, Lua, MetaMethod, Result, String as LuaString, Table, UserData,
6+
UserDataMethods, UserDataRegistry, Value,
77
};
88

99
/// A Lua userdata wrapper around [`Bytes`].
@@ -28,6 +28,10 @@ impl DerefMut for LuaBytes {
2828

2929
impl UserData for LuaBytes {
3030
fn register(registry: &mut UserDataRegistry<Self>) {
31+
registry.add_function("new", |_, data: LuaString| {
32+
Ok(Self(Bytes::copy_from_slice(&data.as_bytes())))
33+
});
34+
3135
registry.add_method("len", |_, this, ()| Ok(this.len()));
3236

3337
registry.add_method("is_empty", |_, this, ()| Ok(this.is_empty()));
@@ -95,3 +99,18 @@ impl Deref for AsBytesRefImpl<'_> {
9599
}
96100
}
97101
}
102+
103+
/// A loader for the `bytes` module.
104+
fn loader(lua: &Lua) -> Result<Table> {
105+
let t = lua.create_table()?;
106+
t.set("Bytes", lua.create_proxy::<LuaBytes>()?)?;
107+
Ok(t)
108+
}
109+
110+
/// Registers the `bytes` module in the given Lua state.
111+
pub fn register(lua: &Lua, name: Option<&str>) -> Result<Table> {
112+
let name = name.unwrap_or("@bytes");
113+
let value = loader(lua)?;
114+
lua.register_module(name, &value)?;
115+
Ok(value)
116+
}

tests/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ async fn run_file(modname: &str) -> Result<()> {
99

1010
// Preload all modules
1111
mlua_stdlib::assertions::register(&lua, None)?;
12+
mlua_stdlib::bytes::register(&lua, None)?;
1213
mlua_stdlib::env::register(&lua, None)?;
1314
let testing = mlua_stdlib::testing::register(&lua, None)?;
1415
mlua_stdlib::time::register(&lua, None)?;
@@ -95,6 +96,7 @@ macro_rules! include_tests {
9596

9697
include_tests! {
9798
assertions,
99+
bytes,
98100
env,
99101
time,
100102
#[cfg(feature = "json")] json,

tests/lua/bytes_tests.lua

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
local bytes = require("@bytes")
2+
3+
testing:test("Bytes.split_off", function(t)
4+
local b = bytes.Bytes.new("hello world")
5+
local other = b:split_off(6)
6+
t.assert_eq(tostring(b), "hello ")
7+
t.assert_eq(tostring(other), "world")
8+
end)
9+
10+
testing:test("Bytes.split_to", function(t)
11+
local b = bytes.Bytes.new("hello world")
12+
local other = b:split_to(5)
13+
t.assert_eq(tostring(b), " world")
14+
t.assert_eq(tostring(other), "hello")
15+
end)
16+
17+
testing:test("Bytes.clear", function(t)
18+
local b = bytes.Bytes.new("hello")
19+
b:clear()
20+
t.assert_eq(b:len(), 0)
21+
t.assert_eq(b:is_empty(), true)
22+
end)
23+
24+
testing:test("Bytes.truncate", function(t)
25+
local b = bytes.Bytes.new("hello")
26+
b:truncate(2)
27+
t.assert_eq(tostring(b), "he")
28+
t.assert_eq(b:len(), 2)
29+
end)

0 commit comments

Comments
 (0)