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

RFC: bit fields and bit matching #29

Closed
wants to merge 3 commits into from

Conversation

farcaller
Copy link

No description provided.

@farcaller farcaller changed the title Added RFC on bit fields RFC: bit fields and bit matching Apr 4, 2014
0b00 => ...,
0b01 => ...,
0b02 => ...,
0b03 => ...
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 and 3 are not valid binary digits. This would have to be 0b10 and 0b11.

```rust
let mut val: u32 = ...;
let bits1 = val[4..5]; // equivalent to bits = (val >> 4) & 3
let bits2 = val[0,4..5]; // equivalent to bits = ((val >> 4) & 3) | (val & 1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous line is fine, but this might be a bit too magical. It’s not obvious that , means |

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still, it would be useful to extract non-continuous bits. Maybe using | instead of , is a better option?

@bill-myers
Copy link

I think that it's not necessary to change the language in this way to support this.

For bit access, it seems to me that a macro would work.

For matching, add a special case to the language so that matching an expression that, after trivial constant propagation/move elimination, is (expr & const), (expr % const), (expr << const), (expr >> const) or a combination of those (and possibly others) is considered exhaustive when all the possible output values are specified.

It's also possible to handle matching with integers of any bit-size, but that doesn't quite handle modulus and shifts and so on without adding those too in the typesystem, which seems worse.

@erickt
Copy link

erickt commented Apr 7, 2014

I'm going to vote against this RFC. While I love the idea of bitstring matching, @LeoTestard's awesome rustlex demonstrates it's possible to build a complex pattern matching macro. I don't really see any disadvantages of it being done externally, so I feel this should be done as an external project.

@nrc
Copy link
Member

nrc commented Apr 28, 2014

I think something along these lines is necessary for systems programming. I don't really care if it is part of the language or a syntax extension, as long as it is nice. I would rather have this done well in the language than done awkwardly with a syntax extension. Given that this is a fairly core feature for our core audience, I don't see the attraction of having it outside the language if there is any reason not to.


```rust
let mut val: u32 = ...;
let bits1 = val[4..5]; // equivalent to bits = (val >> 4) & 3
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you explain this syntax please? Its not clear to me from the examples.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider 123u[32..63]. Would such access compile only on some platforms other than 32-bit?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be limited to strictly sized types, e.g. 123u[32..62] wouldn't work, you must use 123u64[32..62].

@nrc
Copy link
Member

nrc commented Apr 28, 2014

I think it is essential to be refer to groups of bits (not just single bits; its not clear to me if that is possible here) and to name them. Both things are possible in C++. I think there is too much potential for errors without.

There is some good discussion on this reddit thread on what is necessary to have safe and portable bitfields - http://www.reddit.com/r/rust/comments/244yz6/bitfields_in_rust/


# Detailed design

The first part of this RFC is defenition of a bit access for integer types. For the sake of simplicity, only unsigned integer types (uint, u8, u16, u32, u64) are supported.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

only fixed width unsigned integer types (u8, u16, u32, u64)

@dobkeratops
Copy link

over the years i'd done a lot of low level work packing vertex and color formats, building DMA tags and so on.. I'd always been perfectly happy with C/C++ on this front without bitfields (always feared them for portability issues,and just used shifts/masks and abstractions of those). Its a long way from the issues that drove me to Rust.. and I can think of many other things i'd rather have added to the language today. Ints in the generic type params (like C++) would extend what you can do with generic code, eg shift/mask values in constants. (eg, have a smartpointer which is a compressed pointer with a shift value for acessing aligned objects within an arena, ). Better generic type inference (equiv of C++ decltype, even auto return type) would help. HKT. If rust could get something that improves on struct inheritance (generalized delegation of fields and component methods? .. maybe on tuples? .. and coercions ?) ... that would be great too. Even array sugar foo[i,j] would be preferable to dedicated bitfields .. if you get improved [] overloading maybe that type of array sugar could be of use for bitfield access? some people want slice syntax, maybe that would work.

@esbullington
Copy link

I've been playing around using Rust with several binary network protocols. Pattern matching on bits would be incredibly useful. I've experienced no easier parsing of binary wire protocols than when I've used OCaml's bitstring pattern matching syntax extension (based on Erlang's bitstring matching). Pure bliss.

If this can be accomplished using macros, great (I still haven't explored Rust's macro system so I'm not sure). But given the interest in Rust from embedded and systems programmers, I would think this would be a great addition to the core language.

Bitfields would be great, too, particularly for the embedded programmers.

@bstrie
Copy link
Contributor

bstrie commented May 6, 2014

Does the recent bitflags! macro alleviate some of the pressure here?

rust-lang/rust#13072

@emberian
Copy link
Member

emberian commented May 6, 2014

No. Bit fields/bit matching are very unrelated to bitflags.

On Tue, May 6, 2014 at 6:24 PM, Ben Striegel notifications@git.luolix.topwrote:

Does the recent bitflags! macro alleviate some of the pressure here?

rust-lang/rust#13072 rust-lang/rust#13072


Reply to this email directly or view it on GitHubhttps://github.com//pull/29#issuecomment-42367685
.

http://octayn.net/

@glaebhoerl
Copy link
Contributor

I haven't read this in detail yet but I think rust-lang/rust#12642 might be related.

@brson
Copy link
Contributor

brson commented Jun 5, 2014

Thank you for the RFC. Current policy is to not have bitfields in the language itself and use syntax extensions. We currently have a bitflags! extension that can be used for some of these use cases.

Closing.

@brson brson closed this Jun 5, 2014
@pnkfelix
Copy link
Member

pnkfelix commented Jun 5, 2014

To elaborate on @brson's comment: If bitflags! does not cover a use case you care about, we encourage you to work with us to design a syntax extension that does cover your use case.

withoutboats pushed a commit to withoutboats/rfcs that referenced this pull request Jan 15, 2017
No more git dependencies there!

Closes rust-lang#29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.