Impl. Integer#size in Rust
@@ -24,6 +24,10 @@ pub fn init(interp: &Artichoke) -> Result<(), ArtichokeError> {
.borrow_mut()
.add_method("chr", Integer::chr, sys::mrb_args_opt(1));
+ integer
+ .borrow_mut()
+ .add_method("size", Integer::size, sys::mrb_args_none());
+
integer
.borrow()
.define(interp)
@@ -108,4 +112,18 @@ impl Integer {
}
}
}
+
+ pub unsafe extern "C" fn size(mrb: *mut sys::mrb_state, slf: sys::mrb_value) -> sys::mrb_value {
+ let interp = unwrap_interpreter!(mrb);
+ let nil = Value::convert(&interp, None::<Value>);
+ if let Ok(_value) = Int::try_convert(&interp, Value::new(&interp, slf)) {
+ if let Ok(size) = Int::try_from(mem::size_of::<Int>()) {
+ Value::convert(&interp, size).inner()
+ } else {
+ nil.inner()
+ }
+ } else {
+ nil.inner()
+ }
+ }
}
