profile
viewpoint

pubfnbar/rust 0

Empowering everyone to build reliable and efficient software.

pull request commentrust-lang/rust

Implement `Index` and `IndexMut` for arrays

I'd like to clarify something. You might be wondering how it is even possible (for some non-std code) to implement an external trait (Index) for an external type (array) given that even The Book says that the orphan rule dictates that: "But we can’t implement external traits on external types". The answer is that this rule was relaxed by RFC #2451 - Re-Rebalancing Coherence, and you can read more about it here:

pubfnbar

comment created time in a month

issue openedmicrosoft/vscode

Keyboard shortcuts that use alt + some dead key don't work

Issue Type: <b>Bug</b>

I use an Apple Magic keyboard with the standard US keyboard layout, which has dead keys on Alt+e, Alt+i, Alt+n, Alt+u, and Alt+`. Dead keys produce a certain accent character and then combine that accent with the character you type next (if it's possible to combine those two). I've always mapped arrow keys to Alt+j, Alt+k, Alt+l, and Alt+i in Sublime Text so that i can move the cursor without moving my right hand. But with VS Code, mapping Alt+i to the up-arrow doesn't work (nor does mapping from any other dead key). Pressing Alt+i just produces the ˆ accent character no matter what it is mapped to in VS Code.

VS Code version: Code 1.47.3 (91899dcef7b8110878ea59626991a18c8a6a1b3e, 2020-07-23T13:08:29.692Z) OS version: Darwin x64 19.6.0

<details> <summary>System Info</summary>

Item Value
CPUs Intel(R) Core(TM) i5-4570R CPU @ 2.70GHz (4 x 2700)
GPU Status 2d_canvas: enabled<br>flash_3d: enabled<br>flash_stage3d: enabled<br>flash_stage3d_baseline: enabled<br>gpu_compositing: enabled<br>metal: disabled_off<br>multiple_raster_threads: enabled_on<br>oop_rasterization: disabled_off<br>protected_video_decode: unavailable_off<br>rasterization: enabled<br>skia_renderer: disabled_off_ok<br>video_decode: enabled<br>viz_display_compositor: enabled_on<br>viz_hit_test_surface_layer: disabled_off_ok<br>webgl: enabled<br>webgl2: enabled
Load (avg) 2, 2, 2
Memory (System) 8.00GB (0.08GB free)
Process Argv
Screen Reader no
VM 0%

</details><details><summary>Extensions (3)</summary>

Extension Author (truncated) Version
Bookmarks ale 11.3.1
toml be5 0.5.1
rust-analyzer mat 0.2.273

</details> <!-- generated by issue reporter -->

created time in a month

issue commentrust-analyzer/rust-analyzer

Document Symbols listed in reverse order within each scope

If you use the Sublime Text's native Goto Symbol feature via its menu bar item Goto > Goto Symbol... you get the document symbols listed in the following order: Goto Symbol - Sublime Text

pubfnbar

comment created time in 2 months

issue openedrust-analyzer/rust-analyzer

Document Symbols listed in reverse order within each scope

Using:

When you have a Rust Enhanced document open in Sublime Text, and you choose Tools > Command Palette, type LSP: Document Symbols and hit Enter, the symbols are listed in reverse order within each scope like this: LSP Document Symbols - rust-analyzer

created time in 2 months

pull request commentrust-lang/rust

Implement `Index` and `IndexMut` for arrays

It would be interesting to test whether this PR breaks any code in crates.io (but I don't know how to do that). I'm pretty sure that at least the following code would stop compiling:

use std::ops::Index;

enum Idx {
    _0,
    _1,
}

impl<T> Index<Idx> for [T] {
    type Output = T;

    fn index(&self, idx: Idx) -> &T {
        Index::index(self, idx as usize)
    }
}

// This PR would cause there to be a clashing std library implementation of `Index<Idx> for [T; 2]`
impl<T> Index<Idx> for [T; 2] {
    type Output = T;

    fn index(&self, idx: Idx) -> &T {
        unsafe { self.get_unchecked(idx as usize) }
    }
}

fn main() {
    let array = [11, 22];
    let slice: &[u8] = &array;
    
    println!("{:?}", array[Idx::_0]);
    println!("{:?}", slice[Idx::_0]);
}

I don't know if this means that this PR can be merged only in the next Rust Edition?

pubfnbar

comment created time in 2 months

pull request commentrust-lang/rust

Implement `Index` and `IndexMut` for arrays

Just to speculate a bit... If it becomes possible to use generic parameters in evaluating constant expressions, and if a BoundedUsize type (that wraps a bounded usize value) is added to the standard library and we want to be able to use it to index (large enough) arrays without bounds checking, it might look something like this:

pub struct Predicate<const P: bool>;

pub trait Satisfied {}

impl Satisfied for Predicate<true> {}

// Guarantees that if `x` is a `BoundedUsize<A, B>` value, then `x.0 >= A && x.0 <= B && A <= B`
pub struct BoundedUsize<const A: usize, const B: usize>(usize)
where Predicate<{ A <= B }>: Satisfied;

impl<T, const N: usize, const A: usize, const B: usize> Index<BoundedUsize<A, B>> for [T; N]
where Predicate<{ A <= B && B < N }>: Satisfied
{
    type Output = T;

    fn index(&self, index: BoundedUsize<A, B>) -> &T {
        unsafe { self.get_unchecked(index.0) }
    }
}
pubfnbar

comment created time in 2 months

Pull request review commentrust-lang/rust

Implement `Index` and `IndexMut` for arrays

 impl<'a, T, const N: usize> IntoIterator for &'a mut [T; N] {     } } +#[stable(feature = "index_trait_on_arrays", since = "1.47.0")]+impl<T, I, const N: usize> Index<I> for [T; N]+where+    [T]: Index<I>,+{+    type Output = <[T] as Index<I>>::Output;++    #[inline]+    fn index(&self, index: I) -> &Self::Output {+        Index::index(self as &[T], index)+    }+}++#[stable(feature = "index_trait_on_arrays", since = "1.47.0")]+impl<T, I, const N: usize> IndexMut<I> for [T; N]+where+    [T]: IndexMut<I>,+{+    #[inline]+    fn index_mut(&mut self, index: I) -> &mut Self::Output {

I is a type and N is a const, so they can't be compared directly. Also, it's not currently possible to use const generic parameters in const evaluation contexts. If at some point the aforementioned limitation to const generics is lifted and some kind of a bounded integer type or a trait is added to the standard library, then the standard library can also add a specialization for indexing arrays with that bounded type/trait which omits bounds checking.

pubfnbar

comment created time in 2 months

push eventpubfnbar/rust

pubfnbar

commit sha ef61d686043f1df450d5a580978b76af985da6d6

Change bounds to cover all slice index types

view details

push time in 2 months

push eventpubfnbar/rust

pubfnbar

commit sha 1c1d001333f7394ff69e4c0fe551ed10418c4a64

Add the 3rd component to rustc version numbers

view details

push time in 2 months

push eventpubfnbar/rust

pubfnbar

commit sha 049cff108314a87c0bc6319b191f53d12ccb9ce2

Add stability attributes

view details

push time in 2 months

PR opened rust-lang/rust

Implement `Index` and `IndexMut` for arrays

Adds implementations of Index and IndexMut for arrays that simply forward to the slice indexing implementation in order to fix the following problem:

If you implement Index<MyIndexType> for an array, you lose all the other indexing functionality that used to be available to the array via its implicit coercion to a slice. An example of what I'm talking about:

use std::ops::Index;

pub enum MyIndexType {
    _0, _1, _2, _3, _4, _5, _6, _7,
}

impl<T> Index<MyIndexType> for [T; 8] {
    type Output = T;

    fn index(&self, index: MyIndexType) -> &T {
        unsafe { self.get_unchecked(index as usize) }
    }
}

fn main() {
    let array = [11u8; 8];

    println!("{:?}", array[MyIndexType::_0]); // OK
    
    println!("{:?}", array[0usize]); // error[E0277]
    //               ^^^^^^^^^^^^^ `[u8; 10]` cannot be indexed by `usize`
}
+28 -1

0 comment

1 changed file

pr created time in 2 months

push eventpubfnbar/rust

pubfnbar

commit sha a8bdb4c896aaa4ec9c19c2419ed394d173f3e465

Implement Index[Mut] for arrays Adds implementations of `Index` and `IndexMut` for arrays that simply forward to the slice indexing implementation.

view details

push time in 2 months

create barnchpubfnbar/rust

branch : impl-array-indexing

created branch time in 2 months

fork pubfnbar/rust

Empowering everyone to build reliable and efficient software.

https://www.rust-lang.org

fork in 2 months

more