impl_twice

Crates.ioimpl_twice
lib.rsimpl_twice
version0.0.3
sourcesrc
created_at2020-11-10 15:27:09.370271
updated_at2020-11-10 20:55:48.967984
descriptionImplement items on two types with only one impl block
homepage
repositoryhttps://github.com/TrolledWoods/impl_twice
max_upload_size
id310831
size13,210
TrolledWoods (TrolledWoods)

documentation

README

impl_twice

Use the same implementation block for two types

Written using rust, https://www.rust-lang.org/

Examples

When writing rust programs, times come when you need two types, one immutable and one mutable.

It is possible to remove this duplication with DSTs, such as the standard library's slice type, where &[T] and &mut [T] are the immutable/mutable counterparts. However, DSTs cannot be created by the programmer, and therefore they are not always applicable.

When making two very similar types that are just immutable/mutable counterparts to each other, you may have to implement the same things on both of the types. Here is an example of the duplication;

struct WrappedSlice<'a, T>(&'a [T]);
struct WrappedSliceMut<'a, T>(&'a mut [T]);

impl<T> WrappedSlice<'_, T> {
    pub fn inner(&self) -> &'_ [T] {
        self.0
    }

    pub fn get(&self, index: usize) -> Option<&'_ T> {
        self.0.get(index)
    }
}

impl<T> WrappedSliceMut<'_, T> {
    pub fn inner(&self) -> &'_ [T] {
        self.0
    }

    pub fn get(&self, index: usize) -> Option<&'_ T> {
        self.0.get(index)
    }

    pub fn get_mut(&mut self, index: usize) -> Option<&'_ mut T> {
        self.0.get_mut(index)
    }
}

This can be solved by having a way to implement the same items on both types. That's what this crate is designed for! This is equivalent to the above example but implemented with this crate;

struct WrappedSlice<'a, T>(&'a [T]);
struct WrappedSliceMut<'a, T>(&'a mut [T]);

impl_twice! (
    impl<T> WrappedSlice<'_, T>, WrappedSliceMut<'_, T> {
        pub fn inner(&self) -> &'_ [T] {
            self.0
        }

        pub fn get(&self, index: usize) -> Option<&'_ T> {
            self.0.get(index)
        }
    }
);

impl<T> WrappedSliceMut<'_, T> {
    pub fn get_mut(&mut self, index: usize) -> Option<&'_ mut T> {
        self.0.get_mut(index)
    }
}

As you can see, the two methods inner and get that were duplicated are now only implemented once.

Building

Build this like any other rust crate, or add it as a dependency in your project.

Contributing

Feel free to post an issue or a pull request if you have any ideas.

Commit count: 13

cargo fmt