GithubHelp home page GithubHelp logo

stadust / variant-partial-eq Goto Github PK

View Code? Open in Web Editor NEW
2.0 1.0 0.0 21 KB

A derive macro for PartialEq implementations that don't get broken by invariant lifetimes

License: MIT License

Rust 100.00%

variant-partial-eq's Introduction

A #[derive] macro for PartialEq implementations that ignore lifetime variance

codecov

Consider a type such as

pub struct StrWrap<'a> {
    wrapped: &'a str
}

The standard #[derive(PartialEq)] generates an implementation as follows

impl<'a> PartialEq<StrWrap<'a>> for StrWrap<'a> { ... }

This seems to imply that we can only compare StrWraps where the lifetime of the wrapped strings are identical. Nevertheless, code such as

static STATIC_STR: &'static str = "Hello World!";

let local_str = String::from("Hellow World!");
let wrap1: StrWrap<'static> = StrWrap { wrapped: STATIC_STR };
let wrap2 = StrWrap { wrapped: local_str.as_ref() }; // <-- lets call the anonymous lifetime here '1
assert_eq!(wrap1, wrap2);

compiles and runs just fine, even though the wrapped str's clearly have different lifetimes! Why? The lifetime 'a is covariant: If I have some other lifetime 'b: 'a, then any place that wants a 'a happily accepts a 'b. Or, in our specific case, we see that PartialEq wants two StrWrap<'1>, but since 'static outlives '1, the compiler realises that downgrading the 'static to a '1 for the duration of the call to .eq is perfectly fine. This is called subtyping.

However, what if my lifetime is not covariant? This happens for example with lifetimes used in generic associated types (GATs). In this case, the compiler cannot adjust our lifetimes for us, and the above example will indeed fail to compile, as there is no PartialEq implementation for StrWrap<'1> == StrWrap<'static>, only for StrWrap<'1> == StrWrap<'1> and StrWrap<'static> == StrWrap<'static>. However, being unable to compare two values because the lifetimes are different is a bit silly, especially since lifetimes are not actually a thing at runtime.

We can work around this limitation by hand-rolling our own PartialEq instead of using the build-in derive:

impl<'a, 'b> PartialEq<StrWrap<'b>> for StrWrap<'a> {
    fn eq(&self, other: &StrWrap<'b>) -> bool {
        self.wrapped == other.wrapped
    }
}

With this, our example works again, as now StrWrap<'1> == StrWrap<'static> is possible. Generating these generalized PartialEq implementations is exactly the purpose of this crate.

variant-partial-eq's People

Contributors

actions-user avatar stadust avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.