Skip to content

Commit

Permalink
Add reflect_eq (#126)
Browse files Browse the repository at this point in the history
### Checklist

* [x] I have read the [Contributor Guide](../../CONTRIBUTING.md)
* [x] I have read and agree to the [Code of
Conduct](../../CODE_OF_CONDUCT.md)
* [x] I have added a description of my changes and why I'd like them
included in the section below

### Description of Changes

A few times I've needed to compare `&dyn Reflect` values for equality.
Previously I did that by converting both to `Value`s but that might be
expensive so. This `reflect_eq` which walks each value without
allocating.
  • Loading branch information
davidpdrsn authored Sep 11, 2023
1 parent 0071902 commit b35b69a
Show file tree
Hide file tree
Showing 5 changed files with 576 additions and 6 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

# Unreleased

- None.
- **added:** Add `reflect_eq`

# 0.1.15 (08. August, 2023)

Expand Down
16 changes: 12 additions & 4 deletions crates/mirror-mirror-macros/src/derive_reflect/attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ impl ItemAttrs {
.filter(|attr| attr.meta.path().is_ident("reflect"))
.peekable();

let Some(attr) = reflect_attrs.next() else { return Ok(Self::new(docs)) };
let Some(attr) = reflect_attrs.next() else {
return Ok(Self::new(docs));
};

if let Some(next) = reflect_attrs.peek() {
return Err(syn::Error::new_spanned(
Expand Down Expand Up @@ -177,8 +179,12 @@ fn parse_docs(attrs: &[Attribute]) -> Vec<LitStr> {
.filter(|attr| attr.meta.path().is_ident("doc"))
.filter_map(|attr| {
let name_value = attr.meta.require_name_value().ok()?;
let Expr::Lit(lit_expr) = &name_value.value else { return None };
let Lit::Str(lit_str) = &lit_expr.lit else { return None };
let Expr::Lit(lit_expr) = &name_value.value else {
return None;
};
let Lit::Str(lit_str) = &lit_expr.lit else {
return None;
};
Some(lit_str.clone())
})
.collect::<Vec<_>>()
Expand Down Expand Up @@ -296,7 +302,9 @@ impl InnerAttrs {
.filter(|attr| attr.meta.path().is_ident("reflect"))
.peekable();

let Some(attr) = reflect_attrs.next() else { return Ok(Self::new(docs)) };
let Some(attr) = reflect_attrs.next() else {
return Ok(Self::new(docs));
};

if let Some(next) = reflect_attrs.peek() {
return Err(syn::Error::new_spanned(
Expand Down
3 changes: 3 additions & 0 deletions crates/mirror-mirror/src/foreign_impls/via_scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ macro_rules! impl_reflect_via_scalar {
impl Reflect for $ty {
trivial_reflect_methods!();

#[allow(clippy::redundant_closure_call)]
fn reflect_owned(self: Box<Self>) -> ReflectOwned {
ReflectOwned::Scalar(ScalarOwned::from($get_fn(&*self)))
}

#[allow(clippy::redundant_closure_call)]
fn reflect_ref(&self) -> ReflectRef<'_> {
ReflectRef::Scalar(ScalarRef::from($get_fn(self)))
}
Expand All @@ -45,6 +47,7 @@ macro_rules! impl_reflect_via_scalar {
}
}

#[allow(clippy::redundant_closure_call)]
fn to_value(&self) -> Value {
$get_fn(self).to_value()
}
Expand Down
11 changes: 10 additions & 1 deletion crates/mirror-mirror/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,9 @@ pub mod value;
pub mod try_visit;

mod foreign_impls;
mod reflect_eq;

pub use reflect_eq::reflect_eq;

#[cfg(feature = "std")]
#[cfg(test)]
Expand Down Expand Up @@ -535,6 +538,12 @@ impl fmt::Debug for dyn Reflect {
}
}

impl PartialEq for dyn Reflect {
fn eq(&self, other: &Self) -> bool {
reflect_eq(self, other).unwrap_or(false)
}
}

macro_rules! impl_for_core_types {
($($ty:ident)*) => {
$(
Expand Down Expand Up @@ -972,7 +981,7 @@ impl<'a> ReflectRef<'a> {
}

/// An immutable reflected scalar value.
#[derive(Debug, Copy, Clone)]
#[derive(Debug, Copy, Clone, PartialEq)]
#[allow(non_camel_case_types)]
pub enum ScalarRef<'a> {
usize(usize),
Expand Down
Loading

0 comments on commit b35b69a

Please sign in to comment.