PartialEq/Eq/Hash + Any?

Is there a crate that provides an easy wrapper around Any for requiring PartialEq/Eq/Hash support?

Do you really need a wrapper for it? Wouldn't the following do too:

#![feature(trait_alias)]

use std::any::Any;
use std::hash::Hash;

trait HashableAny = Any + PartialEq + Eq + Hash;

(Playground)

Or, if you want to use stable Rust:

use std::any::Any;
use std::hash::Hash;

trait HashableAny: Any + PartialEq + Eq + Hash {}
impl<T> HashableAny for T where
    T: Any + PartialEq + Eq + Hash {}

(Playground)

Assuming that the request for Any in particular is done with the goal to work with type-erased trait objects, note that Eq and Hash aren’t object safe. But you can work around that; I don’t personally know any crates for this either, but an approach like this one should work:

Rust Playground

2 Likes

:scream: I didn't know that. Thanks for pointing it out. That makes my example above useless, of course.

Just insignificantly longer than my (broken) attempt :innocent:. (Edit: actually half of the code is testing, so not really that long.)

Okay, now I understand the wish for a crate.

1 Like

It'd also be nice to throw in ToString while at it, but that's the easy part. :‌p (And we don't need the wrapper itself to support it, thankfully.)

But it'd still be nice to get this packaged as a crate...

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.