remove unwraps

This commit is contained in:
Jaroslaw Konik 2025-05-26 07:00:00 +02:00
parent 10985ca777
commit 783183975a

View file

@ -431,11 +431,19 @@ impl Runtime for RubyRuntime {
fn callback(args: &[magnus::Value]) -> magnus::Value { fn callback(args: &[magnus::Value]) -> magnus::Value {
let ruby = magnus::Ruby::get() let ruby = magnus::Ruby::get()
.expect("Failed to get a handle to Ruby API while processing callback"); .expect("Failed to get a handle to Ruby API while processing callback");
let method_name: magnus::value::StaticSymbol = let method_name: magnus::value::StaticSymbol = ruby
ruby.class_object().funcall("__method__", ()).unwrap(); .class_object()
let method_name = method_name.name().unwrap(); .funcall("__method__", ())
let callbacks = RUBY_CALLBACKS.lock().unwrap(); .expect("Failed to get currently called method name symbol from Ruby");
let f = callbacks.get(method_name).unwrap(); let method_name = method_name
.name()
.expect("Failed to convert method symbol to string");
let callbacks = RUBY_CALLBACKS
.lock()
.expect("Failed to lock callbacks when executing a script callback");
let f = callbacks
.get(method_name)
.expect("No callback found to execute with specified name");
let result = f( let result = f(
(), (),
args.iter() args.iter()
@ -485,11 +493,8 @@ impl Runtime for RubyRuntime {
) -> Result<Self::Value, crate::ScriptingError> { ) -> Result<Self::Value, crate::ScriptingError> {
let value = value.clone(); let value = value.clone();
self.ruby_thread self.execute_in_thread(move |ruby| {
.as_ref() let f: Proc = TryConvert::try_convert(ruby.get_inner(value.0))?;
.unwrap()
.execute(Box::new(move |ruby| {
let f: Proc = TryConvert::try_convert(ruby.get_inner(value.0)).unwrap();
let args: Vec<_> = args let args: Vec<_> = args
.into_iter() .into_iter()
@ -497,7 +502,7 @@ impl Runtime for RubyRuntime {
.collect(); .collect();
let result: magnus::Value = f.funcall("call", args.as_slice())?; let result: magnus::Value = f.funcall("call", args.as_slice())?;
Ok(RubyValue::new(result)) Ok(RubyValue::new(result))
})) })
} }
fn needs_own_thread() -> bool { fn needs_own_thread() -> bool {
@ -516,7 +521,7 @@ pub mod prelude {
impl<T: TryConvert> FromRuntimeValueWithEngine<'_, RubyRuntime> for T { impl<T: TryConvert> FromRuntimeValueWithEngine<'_, RubyRuntime> for T {
fn from_runtime_value_with_engine(value: RubyValue, engine: &magnus::Ruby) -> Self { fn from_runtime_value_with_engine(value: RubyValue, engine: &magnus::Ruby) -> Self {
let inner = engine.get_inner(value.0); let inner = engine.get_inner(value.0);
T::try_convert(inner).unwrap() T::try_convert(inner).expect("Failed to convert from Ruby value to native type")
} }
} }
@ -545,7 +550,8 @@ pub struct RArray(pub Opaque<magnus::RArray>);
impl FromRuntimeValueWithEngine<'_, RubyRuntime> for RArray { impl FromRuntimeValueWithEngine<'_, RubyRuntime> for RArray {
fn from_runtime_value_with_engine(value: RubyValue, engine: &magnus::Ruby) -> Self { fn from_runtime_value_with_engine(value: RubyValue, engine: &magnus::Ruby) -> Self {
let inner = engine.get_inner(value.0); let inner = engine.get_inner(value.0);
let array = magnus::RArray::try_convert(inner).unwrap(); let array =
magnus::RArray::try_convert(inner).expect("Failed to convert Ruby value to RArray");
RArray(Opaque::from(array)) RArray(Opaque::from(array))
} }
} }