Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add faster versions of equals and is_equal
- `Object::equals` is now a wrapper around the `rb_equal` C function;
- `Object::is_equal` simply performs an `==` comparison on the
  underlying values, which is the same behaviour as the `rb_obj_equal` C
  function.
  • Loading branch information
asppsa committed Nov 11, 2019
1 parent 1c951b5 commit 48bfe11
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
4 changes: 4 additions & 0 deletions src/binding/class.rs
Expand Up @@ -122,3 +122,7 @@ pub fn is_frozen(object: Value) -> Value {
pub fn freeze(object: Value) -> Value {
unsafe { class::rb_obj_freeze(object) }
}

pub fn equals(object1: Value, object2: Value) -> Value {
unsafe { class::rb_equal(object1, object2) }
}
14 changes: 3 additions & 11 deletions src/class/traits/object.rs
Expand Up @@ -745,11 +745,7 @@ pub trait Object: From<Value> {
/// a == c # true
/// ```
fn equals<T: Object>(&self, other: &T) -> bool {
let v = self.value();
let m = "==";
let a = [other.value()];

vm::call_method(v, m, &a).is_true()
class::equals(self.value(), other.value()).is_true()
}

/// Alias for Ruby's `===`
Expand Down Expand Up @@ -843,14 +839,10 @@ pub trait Object: From<Value> {
///
///
/// a.equal?(b)
/// a.eqlua?(c)
/// a.equal?(c)
/// ```
fn is_equal<T: Object>(&self, other: &T) -> bool {
let v = self.value();
let m = "equal?";
let a = [other.value()];

vm::call_method(v, m, &a).is_true()
self.value() == other.value()
}

/// Checks whether the object responds to given method
Expand Down
3 changes: 3 additions & 0 deletions src/rubysys/class.rs
Expand Up @@ -43,6 +43,9 @@ extern "C" {
name: *const c_char,
callback: CallbackPtr,
argc: Argc);
// VALUE
// rb_equal(VALUE obj1, VALUE obj2)
pub fn rb_equal(obj1: Value, obj2: Value) -> Value;
// void
// rb_extend_object(VALUE object, VALUE module)
pub fn rb_extend_object(object: Value, module: Value);
Expand Down

0 comments on commit 48bfe11

Please sign in to comment.