Skip to content

Wrap vref in opaque type to allow exhaustive matching on Value enum outside of crate #503

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/state/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use crate::util::{
pop_error, push_internal_userdata, push_string, push_table, rawset_field, safe_pcall, safe_xpcall,
short_type_name, StackGuard, WrappedFailure,
};
use crate::value::{Nil, Value};
use crate::value::{Nil, Other, Value};

use super::extra::ExtraData;
use super::{Lua, LuaOptions, WeakLua};
Expand Down Expand Up @@ -585,7 +585,7 @@ impl RawLua {
let protect = !self.unlikely_memory_error();
push_internal_userdata(state, WrappedFailure::Error(*err.clone()), protect)?;
}
Value::Other(vref) => self.push_ref(vref),
Value::Other(Other { inner: vref }) => self.push_ref(vref),
}
Ok(())
}
Expand Down Expand Up @@ -689,7 +689,9 @@ impl RawLua {

_ => {
ffi::lua_xpush(state, self.ref_thread(), idx);
Value::Other(self.pop_ref_thread())
Value::Other(Other {
inner: self.pop_ref_thread(),
})
}
}
}
Expand Down
17 changes: 11 additions & 6 deletions src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,13 @@ pub enum Value {
/// `Error` is a special builtin userdata type. When received from Lua it is implicitly cloned.
Error(Box<Error>),
/// Any other value not known to mlua (eg. LuaJIT CData).
#[allow(private_interfaces)]
Other(ValueRef),
Other(Other),
}

/// Opaque wrapper to allow matching on `Value::Other` variant
#[derive(Clone)]
pub struct Other {
pub(crate) inner: ValueRef,
}

pub use self::Value::Nil;
Expand Down Expand Up @@ -140,7 +145,7 @@ impl Value {
| Value::Function(Function(vref))
| Value::Thread(Thread(vref, ..))
| Value::UserData(AnyUserData(vref))
| Value::Other(vref) => vref.to_pointer(),
| Value::Other(Other { inner: vref }) => vref.to_pointer(),
#[cfg(feature = "luau")]
Value::Buffer(crate::Buffer(vref)) => vref.to_pointer(),
_ => ptr::null(),
Expand Down Expand Up @@ -179,7 +184,7 @@ impl Value {
| Value::Function(Function(vref))
| Value::Thread(Thread(vref, ..))
| Value::UserData(AnyUserData(vref))
| Value::Other(vref) => unsafe { invoke_to_string(vref) },
| Value::Other(Other { inner: vref }) => unsafe { invoke_to_string(vref) },
#[cfg(feature = "luau")]
Value::Buffer(crate::Buffer(vref)) => unsafe { invoke_to_string(vref) },
Value::Error(err) => Ok(err.to_string()),
Expand Down Expand Up @@ -565,7 +570,7 @@ impl Value {
buf @ Value::Buffer(_) => write!(fmt, "buffer: {:?}", buf.to_pointer()),
Value::Error(e) if recursive => write!(fmt, "{e:?}"),
Value::Error(_) => write!(fmt, "error"),
Value::Other(v) => write!(fmt, "other: {:?}", v.to_pointer()),
Value::Other(Other { inner: v }) => write!(fmt, "other: {:?}", v.to_pointer()),
}
}
}
Expand All @@ -592,7 +597,7 @@ impl fmt::Debug for Value {
#[cfg(feature = "luau")]
Value::Buffer(buf) => write!(fmt, "{buf:?}"),
Value::Error(e) => write!(fmt, "Error({e:?})"),
Value::Other(v) => write!(fmt, "Other({v:?})"),
Value::Other(v) => write!(fmt, "Other({0:?})", v.inner),
}
}
}
Expand Down