Repository: d-unsed/ruru (language: rust)

Issue:
Create Integer struct
Existing API of MRI which is used for both `Fixnum` and `Integer` (`rb_int2inum`, `rb_num2int`) remains the same in 2.4. Thus no changes in `ruby-sys` are required.

Duplicate the contents of `Fixnum` struct to `Integer`.




Discussion / hints:

Fix patch:
diff --git a/src/class/integer.rs b/src/class/integer.rs
new file mode 100644
index 00000000..e5e812e9
--- /dev/null
+++ b/src/class/integer.rs
@@ -0,0 +1,81 @@
+use std::convert::From;
+
+use binding::fixnum;
+use types::{Value, ValueType};
+
+use {Object, VerifiedObject};
+
+/// `Integer`
+#[derive(Debug, PartialEq)]
+pub struct Integer {
+    value: Value,
+}
+
+impl Integer {
+    /// Creates a new `Integer`.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use ruru::{Integer, VM};
+    /// # VM::init();
+    ///
+    /// let integer = Integer::new(1);
+    ///
+    /// assert_eq!(integer.to_i64(), 1);
+    /// ```
+    ///
+    /// Ruby:
+    ///
+    /// ```ruby
+    /// 1 == 1
+    /// ```
+    pub fn new(num: i64) -> Self {
+        Self::from(fixnum::int_to_num(num))
+    }
+
+    /// Retrieves an `i64` value from `Integer`.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use ruru::{Integer, VM};
+    /// # VM::init();
+    ///
+    /// let integer = Integer::new(1);
+    ///
+    /// assert_eq!(integer.to_i64(), 1);
+    /// ```
+    ///
+    /// Ruby:
+    ///
+    /// ```ruby
+    /// 1 == 1
+    /// ```
+    pub fn to_i64(&self) -> i64 {
+        fixnum::num_to_int(self.value())
+    }
+}
+
+impl From<Value> for Integer {
+    fn from(value: Value) -> Self {
+        Integer { value: value }
+    }
+}
+
+impl Object for Integer {
+    #[inline]
+    fn value(&self) -> Value {
+        self.value
+    }
+}
+
+impl VerifiedObject for Integer {
+    fn is_correct_type<T: Object>(object: &T) -> bool {
+        object.value().ty() == ValueType::Fixnum
+    }
+
+    fn error_message() -> &'static str {
+        "Error converting to Integer"
+    }
+}
diff --git a/src/class/mod.rs b/src/class/mod.rs
index 1587c108..3ca3a448 100644
--- a/src/class/mod.rs
+++ b/src/class/mod.rs
@@ -6,6 +6,7 @@ pub mod fixnum;
 pub mod float;
 pub mod gc;
 pub mod hash;
+pub mod integer;
 pub mod nil_class;
 pub mod rproc;
 pub mod string;
diff --git a/src/lib.rs b/src/lib.rs
index 03b9f6e5..5f4af696 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -23,6 +23,7 @@ pub use class::fixnum::Fixnum;
 pub use class::float::Float;
 pub use class::gc::GC;
 pub use class::hash::Hash;
+pub use class::integer::Integer;
 pub use class::nil_class::NilClass;
 pub use class::rproc::Proc;
 pub use class::string::RString;


Test patch: