Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created July 11, 2020 17:14
Show Gist options
  • Save rust-play/c41f8d8cc2fad7226cbe663b728f9fe1 to your computer and use it in GitHub Desktop.
Save rust-play/c41f8d8cc2fad7226cbe663b728f9fe1 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
#![feature(generic_associated_types)]
use core::ops::Index;
use core::ops::IndexMut;
pub trait IndexRef<Idx: ?Sized> {
type Output<'a>: ?Sized;
fn index(&self, index: Idx) -> &Self::Output<'_>;
}
pub trait IndexMutRef<Idx: ?Sized>: IndexRef<Idx> {
fn index_mut(&mut self, index: Idx) -> &mut Self::Output<'_>;
}
pub trait IndexVal<Idx: ?Sized> {
type Output<'a>: ?Sized;
fn index(&self, index: Idx) -> Self::Output<'_>;
}
impl<T: Index<Idx>, Idx> IndexRef<Idx> for T {
type Output<'a> = <Self as Index<Idx>>::Output;
fn index(&self, index: Idx) -> &Self::Output<'_> {
<Self as Index<Idx>>::index(self, index)
}
}
impl<T: IndexMut<Idx>, Idx> IndexMutRef<Idx> for T {
fn index_mut(&mut self, index: Idx) -> &mut Self::Output<'_> {
<Self as IndexMut<Idx>>::index_mut(self, index)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment