Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::ops::Index;
- trait MyTrait {
- fn foo(&self);
- }
- struct TypeA;
- impl MyTrait for TypeA {
- fn foo(&self) {
- println!("Foo A");
- }
- }
- struct TypeB;
- impl MyTrait for TypeB {
- fn foo(&self) {
- println!("Foo B");
- }
- }
- struct Container<'a, T1: 'a, T2: 'a> {
- some_foo: &'a [T1],
- some_more_foo: &'a [T2],
- }
- impl<'a, T1, T2> Index<usize> for Container<'a, T1, T2>
- where
- T1: MyTrait,
- T2: MyTrait,
- {
- type Output = MyTrait;
- fn index(&self, index: usize) -> &MyTrait {
- let foo_len = self.some_foo.len();
- if index < foo_len {
- &self.some_foo[index]
- }
- else {
- &self.some_more_foo[index]
- }
- }
- }
- fn main() {
- let a = [TypeA {}, TypeA {}, TypeA {}];
- let b = [TypeB {}, TypeB {}, TypeB {}];
- let c = Container { some_foo: &a, some_more_foo: &b };
- c[0].foo();
- }
Add Comment
Please, Sign In to add comment