Skip to content

panhania/bytes_of

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 

Repository files navigation

bytes_of

This is a tiny crate that adds a macro for calculating byte offset of given field within a struct. It also defines similar macros for calculating size and alignment.

Usage

struct Foo {
    bar: i16,
    baz: i32,
}

struct Bar {
    quux: i32,
    norf: i64,
}

let baz_offset = offset_of!(Foo, baz);
let norf_offset = offset_of!(Foo, bar.norf);

Why?

Rust has reserved the offsetof keyword but has no use for it right now. Until it gets added to the language we have to deal with it in some other way. And because macros were invented to overcome shortcomings of the built-in syntax, the offset_of! macro has to do for the time being.

Similarly, keywords sizeof and alignof are also reserved but are not part of the language even though functions mem::size_of and mem::align_of exist in the standard library. Because of that macros size_of! and align_of! are completely unnecessary. However, they were added to this crate for consistency: offset_of! is usually used in places where size_of! is needed as well and both of them being used as macros next to each other just feels nicer.

Alternatives

There also exists a field-offset crate that has a bit richer API and might be a better fit for your needs. I, however, wanted something simpler and with interface identical to the one found in other languages.

Acknowledgements

See issue #1144 for discussion about sizeof, alignof and offsetof. The offset_of! macro implementation is based on this thread.

About

Type calculation macros for Rust

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages