A rust crate to view a structure as raw bytes (&[u8])

Overview

rawbytes

A Rust crate to view a structure as a plain byte array (&[u8]).

Super simple. Tiny. Zero dependencies.

This is a safer interface to slice::from_raw_parts_{mut}()

Example usage:

use rawbytes::RawBytes;

#[repr(C, packed(4))])]
struct Foo {
    x: [u32; 32],
}

#[test]
fn test() {
    let mut foo = Foo { x: [0; 32] };

    let foo_bytes = RawBytes::bytes_view(&foo);
    assert_eq!(foo_bytes.len(), 128);

    let foo_bytes = RawBytes::bytes_view_mut(&mut foo);
    foo_bytes[0] = 1;
    assert_eq!(foo.x[0], 1);
}

Note that structures must should have the C representation to ensure that a new Rust release is not going to change the representation.

Warning and alternatives

Warning: this crate contains two instances of the unsafe keyword, because there are no other ways to achieve this in Rust. Still, you may be named and shamed for using a crate that perfectly does the job, but includes that keyword.

An alternative is the zerocopy crate. It's bigger, far more complex, not any faster, and it also require the unsafe keyword. But it's maintained by a Google employee, so you may be less named and shamed.

You might also like...
A small command-line application to view images from the terminal written in Rust.
A small command-line application to view images from the terminal written in Rust.

A small command-line application to view images from the terminal written in Rust. It is basically the front-end of viuer

A discord bot to view & monitor OpenSea collections, written in Rust

Titan What is this This is a discord bot to monitor OpenSea collections and get info about them, including: Floor Price Activity Sales per Hour And mo

The first web framework in Rust to adopt the Model-View-Controller (MVC) architecture

Integra Web Framework Integra is a sleek, performant web framework for Rust, harnessing the power of the hyper library. Will become the first highly p

❗️ Small script to view GitHub notifications in the terminal
❗️ Small script to view GitHub notifications in the terminal

github-notifications Small script to view GitHub notifications in the terminal Shows and color-codes the notification source, eg if you're the owner o

View your github contribution calander in unicode.
View your github contribution calander in unicode.

View your github contribution calander in the terminal Now in rust! gh-cal 0.1.0 marshmallow View your github contribution calander in unicode USAGE:

A simple tui to view & control docker containers
A simple tui to view & control docker containers

oxker A simple tui to view & control docker containers Built in Rust, making heavy use of tui-rs & Bollard Download & install See releases download &

A git sub-command to view your git repository in the web browser
A git sub-command to view your git repository in the web browser

git-view A git sub-command to view your git repository in the web browser! About Are you also frustrated from moving your hands away from the keyboard

Introducing Inlyne, a GPU powered yet browsless tool to help you quickly view markdown files in the blink of an eye.
Introducing Inlyne, a GPU powered yet browsless tool to help you quickly view markdown files in the blink of an eye.

Inlyne - a GPU powered, browserless, markdown + html viewer inlyne README.md --theme dark/light About Markdown files are a wonderful tool to get forma

A formatter for the leptos view! macro

leptosfmt A formatter for the leptos view! macro All notable changes are documented in: CHANGELOG.md Install cargo install leptosfmt or for trying out

Owner
Frank Denis
Get my public keys here: https://sk.tl/7CPRo8kn
Frank Denis
A crate to help you copy things into raw buffers without invoking spooky action at a distance (undefined behavior).

?? presser Utilities to help make copying data around into raw, possibly-uninitialized buffers easier and safer. presser can help you when copying dat

Embark 131 Mar 16, 2023
Pure rust library for reading / writing DNG files providing access to the raw data in a zero-copy friendly way.

DNG-rs   A pure rust library for reading / writing DNG files providing access to the raw data in a zero-copy friendly way. Also containing code for re

apertus° - open source cinema 4 Dec 1, 2022
A raw implementation of Interoperable Private Attribution

Raw IPA A collaborative effort to generate a raw, but broadly functional, prototype of the Interoperable Private Attribution (IPA) proposal. The goal

Martin Thomson 1 Dec 12, 2022
Unopinionated low level API bindings focused on soundness, safety, and stronger types over raw FFI.

?? firehazard ?? Create a fire hazard by locking down your (Microsoft) Windows so nobody can escape (your security sandbox.) Unopinionated low level A

null 5 Nov 17, 2022
Use raw-window-handle 0.5 with crates that depend on 0.4.

OldHasRawWindowHandleWrapper Wrap any type that implements HasRawWindowHandle and HasRawDisplayHandle from raw-window-handle 0.5 in OldHasRawWindowHan

null 1 Nov 25, 2022
Base 32 + 64 encoding and decoding identifiers + bytes in rust, quickly

fast32 Base32 and base64 encoding in Rust. Primarily for integer (u64, u128) and UUID identifiers (behind feature uuid), as well as arbitrary byte arr

Chris Rogus 9 Dec 18, 2023
hexyl is a simple hex viewer for the terminal. It uses a colored output to distinguish different categories of bytes

hexyl is a simple hex viewer for the terminal. It uses a colored output to distinguish different categories of bytes (NULL bytes, printable ASCII characters, ASCII whitespace characters, other ASCII characters and non-ASCII).

David Peter 7.3k Dec 29, 2022
A terminal UI to edit bytes by the nibble.

heh The HEx Helper is a cross-platform terminal UI used for modifying file data in hex or ASCII. It aims to replicate some of the look of hexyl while

nathan 285 Dec 18, 2022
Encode and decode dynamically constructed values of arbitrary shapes to/from SCALE bytes

scale-value · This crate provides a Value type, which is a runtime representation that is compatible with scale_info::TypeDef. It somewhat analogous t

Parity Technologies 15 Jun 24, 2023
Patch binary file using IDA signatures and defined replacement bytes in YAML.

fabricbin Patch binary file using IDA signatures and defined replacement bytes in YAML. Install: cargo install --git https://github.com/makindotcc/fab

makin 3 Oct 24, 2023