Skip to content
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

More natural indexing for BitVec #1

Open
bovee opened this issue Mar 10, 2017 · 1 comment
Open

More natural indexing for BitVec #1

bovee opened this issue Mar 10, 2017 · 1 comment

Comments

@bovee
Copy link
Contributor

bovee commented Mar 10, 2017

It would be nice to write bitvec[1] = true rather than bitvec.set(1, true).

This is essentially blocked on:
rust-lang/rfcs#997

The standard library bit vector has a hacky workaround for getting (and no way to set) indices, but this won't work for us for ranges:
https://doc.rust-lang.org/1.2.0/src/collections/bit.rs.html#168-180

@bovee
Copy link
Contributor Author

bovee commented Dec 18, 2017

The "hacky workaround " above would look like:

impl Index<usize> for BitVec {
    // fairly hacky; it would be nice to have Index<Range>, IndexMut<usize>,
    // and IndexMut<Range> methods too, but since these have to return
    // references that's a little tricky
    type Output = bool;
    fn index(&self, idx: usize) -> &Self::Output {
        const TRUE: &'static bool = &true;
        const FALSE: &'static bool = &false;
        match self.get(idx) {
            true => TRUE,
            false => FALSE,
        }
    }
}

but there's not way to have a compatible setter and this is gross to start with (and slower than the get( method we already have).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants