53 lines
11 KiB
HTML
53 lines
11 KiB
HTML
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="Byte order-aware numeric primitives."><title>zerocopy::byteorder - Rust</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-6b053e98.ttf.woff2,FiraSans-Italic-81dc35de.woff2,FiraSans-Regular-0fe48ade.woff2,FiraSans-MediumItalic-ccf7e434.woff2,FiraSans-Medium-e1aa3f0a.woff2,SourceCodePro-Regular-8badfe75.ttf.woff2,SourceCodePro-Semibold-aa29a496.ttf.woff2".split(",").map(f=>`<link rel="preload" as="font" type="font/woff2"href="../../static.files/${f}">`).join(""))</script><link rel="stylesheet" href="../../static.files/normalize-9960930a.css"><link rel="stylesheet" href="../../static.files/rustdoc-ca0dd0c4.css"><meta name="rustdoc-vars" data-root-path="../../" data-static-root-path="../../static.files/" data-current-crate="zerocopy" data-themes="" data-resource-suffix="" data-rustdoc-version="1.93.1 (01f6ddf75 2026-02-11) (Arch Linux rust 1:1.93.1-1)" data-channel="1.93.1" data-search-js="search-9e2438ea.js" data-stringdex-js="stringdex-a3946164.js" data-settings-js="settings-c38705f0.js" ><script src="../../static.files/storage-e2aeef58.js"></script><script defer src="../sidebar-items.js"></script><script defer src="../../static.files/main-a410ff4d.js"></script><noscript><link rel="stylesheet" href="../../static.files/noscript-263c88ec.css"></noscript><link rel="alternate icon" type="image/png" href="../../static.files/favicon-32x32-eab170b8.png"><link rel="icon" type="image/svg+xml" href="../../static.files/favicon-044be391.svg"></head><body class="rustdoc mod"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><rustdoc-topbar><h2><a href="#">Module byteorder</a></h2></rustdoc-topbar><nav class="sidebar"><div class="sidebar-crate"><h2><a href="../../zerocopy/index.html">zerocopy</a><span class="version">0.8.39</span></h2></div><div class="sidebar-elems"><section id="rustdoc-toc"><h2 class="location"><a href="#">Module byteorder</a></h2><h3><a href="#">Sections</a></h3><ul class="block top-toc"><li><a href="#example" title="Example">Example</a></li></ul><h3><a href="#modules">Module Items</a></h3><ul class="block"><li><a href="#modules" title="Modules">Modules</a></li><li><a href="#structs" title="Structs">Structs</a></li><li><a href="#enums" title="Enums">Enums</a></li><li><a href="#traits" title="Traits">Traits</a></li><li><a href="#types" title="Type Aliases">Type Aliases</a></li></ul></section><div id="rustdoc-modnav"><h2 class="in-crate"><a href="../index.html">In crate zerocopy</a></h2></div></div></nav><div class="sidebar-resizer" title="Drag to resize sidebar"></div><main><div class="width-limiter"><section id="main-content" class="content"><div class="main-heading"><div class="rustdoc-breadcrumbs"><a href="../index.html">zerocopy</a></div><h1>Module <span>byteorder</span> <button id="copy-path" title="Copy item path to clipboard">Copy item path</button></h1><rustdoc-toolbar></rustdoc-toolbar><span class="sub-heading"><a class="src" href="../../src/zerocopy/byteorder.rs.html#9-1563">Source</a> </span></div><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>Byte order-aware numeric primitives.</p>
|
||
<p>This module contains equivalents of the native multi-byte integer types with
|
||
no alignment requirement and supporting byte order conversions.</p>
|
||
<p>For each native multi-byte integer type - <code>u16</code>, <code>i16</code>, <code>u32</code>, etc - and
|
||
floating point type - <code>f32</code> and <code>f64</code> - an equivalent type is defined by
|
||
this module - <a href="struct.U16.html" title="struct zerocopy::byteorder::U16"><code>U16</code></a>, <a href="struct.I16.html" title="struct zerocopy::byteorder::I16"><code>I16</code></a>, <a href="struct.U32.html" title="struct zerocopy::byteorder::U32"><code>U32</code></a>, <a href="struct.F32.html" title="struct zerocopy::byteorder::F32"><code>F32</code></a>, <a href="struct.F64.html" title="struct zerocopy::byteorder::F64"><code>F64</code></a>, etc. Unlike their
|
||
native counterparts, these types have alignment 1, and take a type parameter
|
||
specifying the byte order in which the bytes are stored in memory. Each type
|
||
implements this crate’s relevant conversion and marker traits.</p>
|
||
<p>These two properties, taken together, make these types useful for defining
|
||
data structures whose memory layout matches a wire format such as that of a
|
||
network protocol or a file format. Such formats often have multi-byte values
|
||
at offsets that do not respect the alignment requirements of the equivalent
|
||
native types, and stored in a byte order not necessarily the same as that of
|
||
the target platform.</p>
|
||
<p>Type aliases are provided for common byte orders in the <a href="big_endian/index.html" title="mod zerocopy::byteorder::big_endian"><code>big_endian</code></a>,
|
||
<a href="little_endian/index.html" title="mod zerocopy::byteorder::little_endian"><code>little_endian</code></a>, <a href="network_endian/index.html" title="mod zerocopy::byteorder::network_endian"><code>network_endian</code></a>, and <a href="native_endian/index.html" title="mod zerocopy::byteorder::native_endian"><code>native_endian</code></a> submodules.
|
||
Note that network-endian is a synonym for big-endian.</p>
|
||
<h2 id="example"><a class="doc-anchor" href="#example">§</a>Example</h2>
|
||
<p>One use of these types is for representing network packet formats, such as
|
||
UDP:</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>zerocopy::{<span class="kw-2">*</span>, byteorder::network_endian::U16};
|
||
|
||
<span class="attr">#[derive(FromBytes, IntoBytes, KnownLayout, Immutable, Unaligned)]
|
||
#[repr(C)]
|
||
</span><span class="kw">struct </span>UdpHeader {
|
||
src_port: U16,
|
||
dst_port: U16,
|
||
length: U16,
|
||
checksum: U16,
|
||
}
|
||
|
||
<span class="attr">#[derive(FromBytes, IntoBytes, KnownLayout, Immutable, Unaligned)]
|
||
#[repr(C, packed)]
|
||
</span><span class="kw">struct </span>UdpPacket {
|
||
header: UdpHeader,
|
||
body: [u8],
|
||
}
|
||
|
||
<span class="kw">impl </span>UdpPacket {
|
||
<span class="kw">fn </span>parse(bytes: <span class="kw-2">&</span>[u8]) -> <span class="prelude-ty">Option</span><<span class="kw-2">&</span>UdpPacket> {
|
||
UdpPacket::ref_from_bytes(bytes).ok()
|
||
}
|
||
}</code></pre></div></div></details><h2 id="modules" class="section-header">Modules<a href="#modules" class="anchor">§</a></h2><dl class="item-table"><dt><a class="mod" href="big_endian/index.html" title="mod zerocopy::byteorder::big_endian">big_<wbr>endian</a></dt><dd>Numeric primitives stored in
|
||
big-endian
|
||
byte order.</dd><dt><a class="mod" href="little_endian/index.html" title="mod zerocopy::byteorder::little_endian">little_<wbr>endian</a></dt><dd>Numeric primitives stored in
|
||
little-endian
|
||
byte order.</dd><dt><a class="mod" href="native_endian/index.html" title="mod zerocopy::byteorder::native_endian">native_<wbr>endian</a></dt><dd>Numeric primitives stored in
|
||
native-endian
|
||
byte order.</dd><dt><a class="mod" href="network_endian/index.html" title="mod zerocopy::byteorder::network_endian">network_<wbr>endian</a></dt><dd>Numeric primitives stored in
|
||
network-endian
|
||
byte order.</dd></dl><h2 id="structs" class="section-header">Structs<a href="#structs" class="anchor">§</a></h2><dl class="item-table"><dt><a class="struct" href="struct.F32.html" title="struct zerocopy::byteorder::F32">F32</a></dt><dd>A 32-bit floating point number stored in a given byte order.</dd><dt><a class="struct" href="struct.F64.html" title="struct zerocopy::byteorder::F64">F64</a></dt><dd>A 64-bit floating point number stored in a given byte order.</dd><dt><a class="struct" href="struct.I16.html" title="struct zerocopy::byteorder::I16">I16</a></dt><dd>A 16-bit signed integer stored in a given byte order.</dd><dt><a class="struct" href="struct.I32.html" title="struct zerocopy::byteorder::I32">I32</a></dt><dd>A 32-bit signed integer stored in a given byte order.</dd><dt><a class="struct" href="struct.I64.html" title="struct zerocopy::byteorder::I64">I64</a></dt><dd>A 64-bit signed integer stored in a given byte order.</dd><dt><a class="struct" href="struct.I128.html" title="struct zerocopy::byteorder::I128">I128</a></dt><dd>A 128-bit signed integer stored in a given byte order.</dd><dt><a class="struct" href="struct.Isize.html" title="struct zerocopy::byteorder::Isize">Isize</a></dt><dd>A word-sized signed integer stored in a given byte order.</dd><dt><a class="struct" href="struct.U16.html" title="struct zerocopy::byteorder::U16">U16</a></dt><dd>A 16-bit unsigned integer stored in a given byte order.</dd><dt><a class="struct" href="struct.U32.html" title="struct zerocopy::byteorder::U32">U32</a></dt><dd>A 32-bit unsigned integer stored in a given byte order.</dd><dt><a class="struct" href="struct.U64.html" title="struct zerocopy::byteorder::U64">U64</a></dt><dd>A 64-bit unsigned integer stored in a given byte order.</dd><dt><a class="struct" href="struct.U128.html" title="struct zerocopy::byteorder::U128">U128</a></dt><dd>A 128-bit unsigned integer stored in a given byte order.</dd><dt><a class="struct" href="struct.Usize.html" title="struct zerocopy::byteorder::Usize">Usize</a></dt><dd>A word-sized unsigned integer stored in a given byte order.</dd></dl><h2 id="enums" class="section-header">Enums<a href="#enums" class="anchor">§</a></h2><dl class="item-table"><dt><a class="enum" href="enum.BigEndian.html" title="enum zerocopy::byteorder::BigEndian">BigEndian</a></dt><dd>Big-endian byte order.</dd><dt><a class="enum" href="enum.LittleEndian.html" title="enum zerocopy::byteorder::LittleEndian">Little<wbr>Endian</a></dt><dd>Little-endian byte order.</dd></dl><h2 id="traits" class="section-header">Traits<a href="#traits" class="anchor">§</a></h2><dl class="item-table"><dt><a class="trait" href="trait.ByteOrder.html" title="trait zerocopy::byteorder::ByteOrder">Byte<wbr>Order</a></dt><dd>A type-level representation of byte order.</dd></dl><h2 id="types" class="section-header">Type Aliases<a href="#types" class="anchor">§</a></h2><dl class="item-table"><dt><a class="type" href="type.BE.html" title="type zerocopy::byteorder::BE">BE</a></dt><dd>A type alias for <a href="enum.BigEndian.html" title="enum zerocopy::byteorder::BigEndian"><code>BigEndian</code></a>.</dd><dt><a class="type" href="type.LE.html" title="type zerocopy::byteorder::LE">LE</a></dt><dd>A type alias for <a href="enum.LittleEndian.html" title="enum zerocopy::byteorder::LittleEndian"><code>LittleEndian</code></a>.</dd><dt><a class="type" href="type.NativeEndian.html" title="type zerocopy::byteorder::NativeEndian">Native<wbr>Endian</a></dt><dd>The endianness used by this platform.</dd><dt><a class="type" href="type.NetworkEndian.html" title="type zerocopy::byteorder::NetworkEndian">Network<wbr>Endian</a></dt><dd>The endianness used in many network protocols.</dd></dl></section></div></main></body></html> |