diff options
author | kyren <kerriganw@gmail.com> | 2017-06-24 18:11:56 -0400 |
---|---|---|
committer | kyren <kerriganw@gmail.com> | 2017-06-24 18:11:56 -0400 |
commit | 47db72cac47bf6fc3d09fa0747997a102d41df9c (patch) | |
tree | e894daa455772db34cbc1a301cd030a0d947ea17 /src/conversion.rs | |
parent | 802d5467c19328974cd63a868b913512a9debc3a (diff) | |
download | mlua-47db72cac47bf6fc3d09fa0747997a102d41df9c.zip |
Big API incompatible error change, remove dependency on error_chain
The current situation with error_chain is less than ideal, and there are lots of
conflicting interests that are impossible to meet at once. Here is an
unorganized brain dump of the current situation, stay awhile and listen!
This change was triggered ultimately by the desire to make LuaError implement
Clone, and this is currently impossible with error_chain. LuaError must
implement Clone to be a proper lua citizen that can live as userdata within a
lua runtime, because there is no way to limit what the lua runtime can do with a
received error. Currently, this is solved by there being a rule that the error
will "expire" if the error is passed back into rust, and this is very
sub-optimal. In fact, one could easily imagine a scenario where lua is for
example memoizing some function, and if the function has ever errored in the
past the function should continue returning the same error, and this situation
immediately fails with this restriciton in place.
Additionally, there are other more minor problems with error_chain which make
the API less good than it could be, or limit how we can use error_chain. This
change has already solved a small bug in a Chucklefish project, where the
conversion from an external error type (Borrow[Mut]Error) was allowed but not
intended for user code, and was accidentally used. Additionally, pattern
matching on error_chain errors, which should be common when dealing with Lua, is
less convenient than a hand rolled error type.
So, if we decide not to use error_chain, we now have a new set of problems if we
decide interoperability with error_chain is important. The first problem we run
into is that there are two natural bounds for wrapped errors that we would
pick, (Error + Send + Sync), or just Error, and neither of them will
interoperate well with error_chain. (Error + Send + Sync) means we can't wrap
error chain errors into LuaError::ExternalError (they're missing the Sync
bound), and having the bounds be just Error means the opposite, that we can't
hold a LuaError inside an error_chain error.
We could just decide that interoperability with error_chain is the most
important qualification, and pick (Error + Send), but this causes a DIFFERENT
set of problems. The rust ecosystem has the two primary error bounds as Error
or (Error + Send + Sync), and there are Into impls from &str / String to
Box<Error + Send + Sync> for example, but NOT (Error + Send). This means that
we are forced to manually recreate the conversions from &str / String to
LuaError rather than relying on a single Into<Box<Error + Send + Sync>> bound,
but this means that string conversions have a different set of methods than
other error types for external error conversion. I have not been able to figure
out an API that I am happy with that uses the (Error + Send) bound. Box<Error>
is obnoxious because not having errors implement Send causes needless problems
in a multithreaded context, so that leaves (Error + Send + Sync). This is
actually a completely reasonable bound for external errors, and has the nice
String Into impls that we would want, the ONLY problem is that it is a pain to
interoperate with the current version of error_chain.
It would be nice to be able to specify the traits that an error generated by the
error_chain macro would implement, and this is apparently in progress in the
error_chain library. This would solve both the problem with not being able to
implement Clone and the problems with (Error + Send) bounds. I am not convinced
that this library should go back to using error_chain when that functionality is
in stable error_chain though, because of the other minor usability problems with
using error_chain.
In that theoretical situation, the downside of NOT using error_chain is simply
that there would not be automatic stacktraces of LuaError. This is not a huge
problem, because stack traces of lua errors are not extremely useful, and for
external errors it is not too hard to create a different version of the
LuaExternalResult / LuaExternalError traits and do conversion from an
error_chain type into a type that will print the stacktrace on display, or
use downcasting in the error causes.
So in summary, this library is no longer using error_chain, and probably will
not use it again in the future. Currently this means that to interoperate with
error_chain, you should use error_chain 0.8.1, which derives Sync on errors, or
wait for a version that supports user defined trait derives. In the future
when error_chain supports user defined trait derives, users may have to take an
extra step to make wrapped external errors print the stacktrace that they
capture.
This change works, but is not entirely complete. There is no error
documentation yet, and the change brought to a head an ugly module organization
problem. There will be more commits for documentation and reorganization, then
a new stable version of rlua.
Diffstat (limited to 'src/conversion.rs')
-rw-r--r-- | src/conversion.rs | 36 |
1 files changed, 27 insertions, 9 deletions
diff --git a/src/conversion.rs b/src/conversion.rs index 5ef0050..42b3bfa 100644 --- a/src/conversion.rs +++ b/src/conversion.rs @@ -38,7 +38,9 @@ impl<'lua> FromLua<'lua> for LuaTable<'lua> { fn from_lua(value: LuaValue<'lua>, _: &'lua Lua) -> LuaResult<LuaTable<'lua>> { match value { LuaValue::Table(table) => Ok(table), - _ => Err("cannot convert lua value to table".into()), + _ => Err(LuaError::ConversionError( + "cannot convert lua value to table".to_owned(), + )), } } } @@ -53,7 +55,9 @@ impl<'lua> FromLua<'lua> for LuaFunction<'lua> { fn from_lua(value: LuaValue<'lua>, _: &'lua Lua) -> LuaResult<LuaFunction<'lua>> { match value { LuaValue::Function(table) => Ok(table), - _ => Err("cannot convert lua value to function".into()), + _ => Err(LuaError::ConversionError( + "cannot convert lua value to function".to_owned(), + )), } } } @@ -68,7 +72,9 @@ impl<'lua> FromLua<'lua> for LuaUserData<'lua> { fn from_lua(value: LuaValue<'lua>, _: &'lua Lua) -> LuaResult<LuaUserData<'lua>> { match value { LuaValue::UserData(ud) => Ok(ud), - _ => Err("cannot convert lua value to userdata".into()), + _ => Err(LuaError::ConversionError( + "cannot convert lua value to userdata".to_owned(), + )), } } } @@ -83,7 +89,9 @@ impl<'lua> FromLua<'lua> for LuaThread<'lua> { fn from_lua(value: LuaValue<'lua>, _: &'lua Lua) -> LuaResult<LuaThread<'lua>> { match value { LuaValue::Thread(t) => Ok(t), - _ => Err("cannot convert lua value to thread".into()), + _ => Err(LuaError::ConversionError( + "cannot convert lua value to thread".to_owned(), + )), } } } @@ -98,7 +106,9 @@ impl<'lua, T: LuaUserDataType + Copy> FromLua<'lua> for T { fn from_lua(value: LuaValue<'lua>, _: &'lua Lua) -> LuaResult<T> { match value { LuaValue::UserData(ud) => Ok(*ud.borrow::<T>()?), - _ => Err("cannot convert lua value to userdata".into()), + _ => Err(LuaError::ConversionError( + "cannot convert lua value to userdata".to_owned(), + )), } } } @@ -129,7 +139,9 @@ impl<'lua> FromLua<'lua> for LightUserData { fn from_lua(v: LuaValue, _: &'lua Lua) -> LuaResult<Self> { match v { LuaValue::LightUserData(ud) => Ok(ud), - _ => Err("cannot convert lua value to lightuserdata".into()), + _ => Err(LuaError::ConversionError( + "cannot convert lua value to lightuserdata".to_owned(), + )), } } } @@ -209,7 +221,9 @@ impl<'lua, T: FromLua<'lua>> FromLua<'lua> for Vec<T> { if let LuaValue::Table(table) = value { table.sequence_values().collect() } else { - Err("cannot convert lua value to table for Vec".into()) + Err(LuaError::ConversionError( + "cannot convert lua value to table for Vec".to_owned(), + )) } } } @@ -225,7 +239,9 @@ impl<'lua, K: Eq + Hash + FromLua<'lua>, V: FromLua<'lua>> FromLua<'lua> for Has if let LuaValue::Table(table) = value { table.pairs().collect() } else { - Err("cannot convert lua value to table for HashMap".into()) + Err(LuaError::ConversionError( + "cannot convert lua value to table for HashMap".to_owned(), + )) } } } @@ -241,7 +257,9 @@ impl<'lua, K: Ord + FromLua<'lua>, V: FromLua<'lua>> FromLua<'lua> for BTreeMap< if let LuaValue::Table(table) = value { table.pairs().collect() } else { - Err("cannot convert lua value to table for BTreeMap".into()) + Err(LuaError::ConversionError( + "cannot convert lua value to table for BTreeMap".to_owned(), + )) } } } |