-
-
Save rust-play/c41f8d8cc2fad7226cbe663b728f9fe1 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![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