internal: add index to minicore

This commit is contained in:
Aleksey Kladov 2021-06-18 22:37:03 +03:00
parent 73b3ee664e
commit 991919e71f
2 changed files with 50 additions and 38 deletions

View file

@ -567,11 +567,11 @@ fn indexing_arrays() {
fn infer_ops_index() {
check_types(
r#"
//- /main.rs crate:main deps:std
//- minicore: index
struct Bar;
struct Foo;
impl std::ops::Index<u32> for Bar {
impl core::ops::Index<u32> for Bar {
type Output = Foo;
}
@ -580,15 +580,6 @@ fn test() {
let b = a[1u32];
b;
} //^ Foo
//- /std.rs crate:std
#[prelude_import] use ops::*;
mod ops {
#[lang = "index"]
pub trait Index<Idx> {
type Output;
}
}
"#,
);
}
@ -597,16 +588,16 @@ mod ops {
fn infer_ops_index_int() {
check_types(
r#"
//- /main.rs crate:main deps:std
//- minicore: index
struct Bar;
struct Foo;
impl std::ops::Index<u32> for Bar {
impl core::ops::Index<u32> for Bar {
type Output = Foo;
}
struct Range;
impl std::ops::Index<Range> for Bar {
impl core::ops::Index<Range> for Bar {
type Output = Bar;
}
@ -616,15 +607,6 @@ fn test() {
b;
//^ Foo
}
//- /std.rs crate:std
#[prelude_import] use ops::*;
mod ops {
#[lang = "index"]
pub trait Index<Idx> {
type Output;
}
}
"#,
);
}
@ -633,25 +615,12 @@ mod ops {
fn infer_ops_index_autoderef() {
check_types(
r#"
//- /main.rs crate:main deps:std
//- minicore: index, slice
fn test() {
let a = &[1u32, 2, 3];
let b = a[1u32];
let b = a[1];
b;
} //^ u32
//- /std.rs crate:std
impl<T> ops::Index<u32> for [T] {
type Output = T;
}
#[prelude_import] use ops::*;
mod ops {
#[lang = "index"]
pub trait Index<Idx> {
type Output;
}
}
"#,
);
}

View file

@ -15,6 +15,7 @@
//! range:
//! deref: sized
//! deref_mut: deref
//! index: sized
//! fn:
//! pin:
//! future: pin
@ -167,6 +168,48 @@ pub mod ops {
};
// endregion:deref
// region:index
mod index {
#[lang = "index"]
pub trait Index<Idx: ?Sized> {
type Output: ?Sized;
fn index(&self, index: Idx) -> &Self::Output;
}
#[lang = "index_mut"]
pub trait IndexMut<Idx: ?Sized>: Index<Idx> {
fn index_mut(&mut self, index: Idx) -> &mut Self::Output;
}
// region:slice
impl<T, I> Index<I> for [T]
where
I: SliceIndex<[T]>,
{
type Output = I::Output;
fn index(&self, index: I) -> &I::Output {
loop {}
}
}
impl<T, I> IndexMut<I> for [T]
where
I: SliceIndex<[T]>,
{
fn index_mut(&mut self, index: I) -> &mut I::Output {
loop {}
}
}
pub unsafe trait SliceIndex<T: ?Sized> {
type Output: ?Sized;
}
unsafe impl<T> SliceIndex<[T]> for usize {
type Output = T;
}
// endregion:slice
}
pub use self::index::{Index, IndexMut};
// endregion:index
// region:range
mod range {
#[lang = "RangeFull"]