|
1 | | -use std::ops::Deref; |
| 1 | +use std::ops::{Deref, DerefMut}; |
2 | 2 |
|
| 3 | +use bytes::Bytes; |
3 | 4 | use mlua::{ |
4 | | - BorrowedBytes, Error, FromLua, Lua, MaybeSend, Result, String as LuaString, UserData, UserDataRef, Value, |
| 5 | + BorrowedBytes, Error, FromLua, Lua, MetaMethod, Result, String as LuaString, UserData, UserDataMethods, |
| 6 | + UserDataRegistry, Value, |
5 | 7 | }; |
6 | 8 |
|
7 | | -/// A wrapper around a byte slice that can be passed to Lua as userdata. |
8 | | -#[cfg(not(feature = "send"))] |
9 | | -pub struct BytesBox(Box<dyn AsRef<[u8]>>); |
| 9 | +/// A Lua userdata wrapper around [`Bytes`]. |
| 10 | +#[derive(Clone, Default, Debug, FromLua, PartialEq, Eq, Hash, Ord, PartialOrd)] |
| 11 | +pub struct LuaBytes(pub Bytes); |
10 | 12 |
|
11 | | -/// A wrapper around a byte slice that can be passed to Lua as userdata. |
12 | | -#[cfg(feature = "send")] |
13 | | -pub struct BytesBox(Box<dyn AsRef<[u8]> + Send>); |
| 13 | +impl Deref for LuaBytes { |
| 14 | + type Target = Bytes; |
14 | 15 |
|
15 | | -impl<T: AsRef<[u8]> + MaybeSend + 'static> From<T> for BytesBox { |
16 | | - #[inline(always)] |
17 | | - fn from(value: T) -> Self { |
18 | | - Self(Box::new(value)) |
| 16 | + #[inline] |
| 17 | + fn deref(&self) -> &Self::Target { |
| 18 | + &self.0 |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +impl DerefMut for LuaBytes { |
| 23 | + #[inline] |
| 24 | + fn deref_mut(&mut self) -> &mut Self::Target { |
| 25 | + &mut self.0 |
19 | 26 | } |
20 | 27 | } |
21 | 28 |
|
22 | | -impl UserData for BytesBox {} |
| 29 | +impl UserData for LuaBytes { |
| 30 | + fn register(registry: &mut UserDataRegistry<Self>) { |
| 31 | + registry.add_method("len", |_, this, ()| Ok(this.len())); |
| 32 | + |
| 33 | + registry.add_method("is_empty", |_, this, ()| Ok(this.is_empty())); |
| 34 | + |
| 35 | + registry.add_method_mut("split_off", |_, this, n| Ok(Self(this.split_off(n)))); |
| 36 | + registry.add_method_mut("split_to", |_, this, n| Ok(Self(this.split_to(n)))); |
| 37 | + |
| 38 | + registry.add_method_mut("clear", |_, this, ()| { |
| 39 | + this.clear(); |
| 40 | + Ok(()) |
| 41 | + }); |
| 42 | + registry.add_method_mut("truncate", |_, this, len| { |
| 43 | + this.truncate(len); |
| 44 | + Ok(()) |
| 45 | + }); |
| 46 | + |
| 47 | + registry.add_meta_method(MetaMethod::ToString, |lua, this, ()| lua.create_string(&this.0)); |
| 48 | + } |
| 49 | +} |
23 | 50 |
|
24 | | -/// A type that can represent either a Lua string or a `BytesBox` userdata. |
| 51 | +/// A type that can represent either a Lua string or a [`LuaBytes`] userdata. |
25 | 52 | pub enum StringOrBytes { |
26 | 53 | String(LuaString), |
27 | | - Bytes(UserDataRef<BytesBox>), |
| 54 | + Bytes(LuaBytes), |
28 | 55 | } |
29 | 56 |
|
30 | 57 | impl FromLua for StringOrBytes { |
31 | 58 | fn from_lua(value: Value, _lua: &Lua) -> Result<Self> { |
32 | 59 | match value { |
33 | 60 | Value::String(s) => Ok(Self::String(s)), |
34 | | - Value::UserData(ud) => Ok(Self::Bytes(ud.borrow::<BytesBox>()?)), |
| 61 | + Value::UserData(ud) => Ok(Self::Bytes(ud.borrow::<LuaBytes>()?.clone())), |
35 | 62 | _ => Err(Error::FromLuaConversionError { |
36 | 63 | from: value.type_name(), |
37 | | - to: "string or bytes".into(), |
| 64 | + to: "String or Bytes".into(), |
38 | 65 | message: None, |
39 | 66 | }), |
40 | 67 | } |
41 | 68 | } |
42 | 69 | } |
43 | 70 |
|
44 | 71 | impl StringOrBytes { |
| 72 | + /// Get a type that dereferences to a underlying byte slice. |
45 | 73 | #[inline] |
46 | | - pub(crate) fn as_bytes_deref(&self) -> impl Deref<Target = [u8]> { |
| 74 | + pub fn as_bytes_deref(&self) -> impl Deref<Target = [u8]> { |
47 | 75 | match self { |
48 | 76 | StringOrBytes::String(s) => AsBytesRefImpl::Lua(s.as_bytes()), |
49 | | - StringOrBytes::Bytes(b) => AsBytesRefImpl::Ref((*b.0).as_ref()), |
| 77 | + StringOrBytes::Bytes(b) => AsBytesRefImpl::Ref(b.as_ref()), |
50 | 78 | } |
51 | 79 | } |
52 | 80 | } |
|
0 commit comments