};
}
+/// This is a version of `instantiate_magic` without the implicit return when an error happens.
+/// Instead, this yields a `Result<Value, Error>`.
+///
+/// For a usage example see the [`magic`](crate::magic) module documentation.
+#[macro_export]
+macro_rules! instantiate_magic_result {
+ ($class:expr, $magic:expr => $value:expr) => {{
+ let value = $crate::Value::new_hash();
+ let this = $crate::Value::new_ref(&value);
+ match this.bless_sv($class) {
+ Err(err) => Err(err),
+ Ok(_) => {
+ value.add_magic($magic.with_value($value));
+ Ok(this)
+ }
+ }
+ }};
+}
+
/// Create an empty hash ref with magic data. This is a convenience helper for `sub new`
/// implementations.
///
#[macro_export]
macro_rules! instantiate_magic {
($class:expr, $magic:expr => $value:expr) => {{
- let value = $crate::Value::new_hash();
- let this = $crate::Value::new_ref(&value);
- this.bless_sv($class)?;
- value.add_magic($magic.with_value($value));
- this
+ $crate::instantiate_magic_result!($class, $magic => $value)?
}};
}