-
Notifications
You must be signed in to change notification settings - Fork 13.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Non-uint array indexing #18878
Comments
I was also asking around irc about this, but got no answers. Here is the current implementation for [T], impl<T> ops::Index<uint, T> for [T] {
fn index(&self, &index: &uint) -> &T {
assert!(index < self.len());
unsafe { mem::transmute(self.repr().data.offset(index as int)) }
}
} Notice that the index is actually being converted to int! It would be nice if we instead had something like, impl<K: Unsigned, T> ops::Index<K, T> for [T] {
fn index(&self, &index: &K) -> &T {
assert!(index < self.len());
unsafe { mem::transmute(self.repr().data.offset(index as int)) }
}
} |
@Grieverheart : yes, that would be cool. |
@Yamakaky it might be done in order to avoid overflows |
Here, it is not a problem as u16 < uint. |
yes, u16 would overflow before uint does. But why would you need u16 as indexes ? As the sole point of int and uint are for size/indexes and pointers. |
@gamazeps The primary issue, at least for me, is that is is annoying to explicitly cast each index. If you're programming e.g. an emulator, the code ends up being too terse. The same basically applies to other operations such as |
self.freqs[FREQ[(CLASS[buf[0] as uint] << 3 | CLASS[buf[1] as uint]) as uint] as uint] += 1;
self.freqs[FREQ[(CLASS[buf[1] as uint] << 3 | CLASS[buf[2] as uint]) as uint] as uint] += 1;
self.freqs[FREQ[(CLASS[buf[2] as uint] << 3 | CLASS[buf[3] as uint]) as uint] as uint] += 1;
self.freqs[FREQ[(CLASS[buf[3] as uint] << 3 | CLASS[buf[4] as uint]) as uint] as uint] += 1;
self.freqs[FREQ[(CLASS[buf[4] as uint] << 3 | CLASS[buf[5] as uint]) as uint] as uint] += 1;
self.freqs[FREQ[(CLASS[buf[5] as uint] << 3 | CLASS[buf[6] as uint]) as uint] as uint] += 1;
self.freqs[FREQ[(CLASS[buf[6] as uint] << 3 | CLASS[buf[7] as uint]) as uint] as uint] += 1; Sounds like a good idea. |
The real issue here is that integral coercions that never overflow should be implicit. That’s rust-lang/rfcs#225, so I think all discussion should be moved there (or at least to the rfcs repo) because this is discussing a language feature, not a bug, and all language feature requests belong as issues or pull requests in the rfcs repo. |
@gamazeps I'm working on an emulator, with a [u16, 0x10000] memory array, and I would like to do something like @P1start ok |
@Yamakaky Can you use a custom struct with operator overloading for now? use std::ops::Index;
struct Mem {
mem: [u16, .. 0x10000]
}
impl Mem {
fn new() -> Mem {
Mem { mem: [0, .. 0x10000] }
}
}
impl Index<u16, u16> for Mem {
fn index<'a> (&'a self, index: &u16) -> &'a u16 {
&self.mem[*index as uint]
}
}
fn main() {
let mem = Mem::new();
println!("{}", mem[mem[0]]);
} |
That's not perfect, but it will play it role, thanks ! |
Yes, closing in favor of the RFC |
manual: Document all rust-project.json fields
Hi
Currently, rust doesn't seem to accept non uint types as index for an array. As an example, this code doesn't compile :
I have to do
Couldn't rustc automate this conversion ?
The text was updated successfully, but these errors were encountered: