# [−][src]Crate memoffset

A crate used for calculating offsets of struct members and their spans.

Some of the funcationality of the crate makes no sense when used along with structs that are not `#[repr(C, packed)]`, but it is up to the user to make sure that they are.

## Examples

    #[macro_use]
    extern crate memoffset;

    #[repr(C, packed)]
    struct HelpMeIAmTrappedInAStructFactory {
        help_me_before_they_: [u8; 15],
        a: u32
    }

    fn main() {
        assert_eq!(offset_of!(HelpMeIAmTrappedInAStructFactory, a), 15);
        assert_eq!(span_of!(HelpMeIAmTrappedInAStructFactory, a), 15..19);
        assert_eq!(span_of!(HelpMeIAmTrappedInAStructFactory, help_me_before_they_[2] .. a), 2..15);
    }

This functionality can be useful, for example, for checksum calculations:

ⓘThis example is not tested

    #[repr(C, packed)]
    struct Message {
        header: MessageHeader,
        fragment_index: u32,
        fragment_count: u32,
        payload: [u8; 1024],
        checksum: u16
    }

    let checksum_range = &raw[span_of!(Message, header..checksum)];
    let checksum = crc16(checksum_range);

## Macros

offset_of | Calculates the offset of the specified field from the start of the struct. This macro supports arbitrary amount of subscripts and recursive member-accesses.
span_of | Produces a range instance representing the sub-slice containing the specified member.

# Help

## Keyboard Shortcuts

`?`
  Show this help dialog
`S`
  Focus the search field
`↑`
  Move up in search results
`↓`
  Move down in search results
`↹`
  Switch tab
`⏎`
  Go to active search result
`+`
  Expand all sections
`-`
  Collapse all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g., `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g., `vec -> usize` or `* -> vec`)

Search multiple things at once by splitting your query with comma (e.g., `str,u8` or `String,struct:Vec,test`)