209 lines
100 KiB
HTML
209 lines
100 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="API documentation for the Rust `PrimInt` trait in crate `num_traits`."><meta name="keywords" content="rust, rustlang, rust-lang, PrimInt"><title>num_traits::int::PrimInt - Rust</title><link rel="stylesheet" type="text/css" href="../../normalize.css"><link rel="stylesheet" type="text/css" href="../../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" type="text/css" href="../../dark.css"><link rel="stylesheet" type="text/css" href="../../light.css" id="themeStyle"><script src="../../storage.js"></script><noscript><link rel="stylesheet" href="../../noscript.css"></noscript><link rel="shortcut icon" href="../../favicon.ico"><style type="text/css">#crate-search{background-image:url("../../down-arrow.svg");}</style></head><body class="rustdoc trait"><!--[if lte IE 8]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="sidebar"><div class="sidebar-menu">☰</div><a href='../../num_traits/index.html'><div class='logo-container'><img src='../../rust-logo.png' alt='logo'></div></a><p class='location'>Trait PrimInt</p><div class="sidebar-elems"><div class="block items"><a class="sidebar-title" href="#required-methods">Required Methods</a><div class="sidebar-links"><a href="#tymethod.count_ones">count_ones</a><a href="#tymethod.count_zeros">count_zeros</a><a href="#tymethod.from_be">from_be</a><a href="#tymethod.from_le">from_le</a><a href="#tymethod.leading_zeros">leading_zeros</a><a href="#tymethod.pow">pow</a><a href="#tymethod.rotate_left">rotate_left</a><a href="#tymethod.rotate_right">rotate_right</a><a href="#tymethod.signed_shl">signed_shl</a><a href="#tymethod.signed_shr">signed_shr</a><a href="#tymethod.swap_bytes">swap_bytes</a><a href="#tymethod.to_be">to_be</a><a href="#tymethod.to_le">to_le</a><a href="#tymethod.trailing_zeros">trailing_zeros</a><a href="#tymethod.unsigned_shl">unsigned_shl</a><a href="#tymethod.unsigned_shr">unsigned_shr</a></div><a class="sidebar-title" href="#foreign-impls">Implementations on Foreign Types</a><div class="sidebar-links"><a href="#impl-PrimInt-for-i128">i128</a><a href="#impl-PrimInt-for-i16">i16</a><a href="#impl-PrimInt-for-i32">i32</a><a href="#impl-PrimInt-for-i64">i64</a><a href="#impl-PrimInt-for-i8">i8</a><a href="#impl-PrimInt-for-isize">isize</a><a href="#impl-PrimInt-for-u128">u128</a><a href="#impl-PrimInt-for-u16">u16</a><a href="#impl-PrimInt-for-u32">u32</a><a href="#impl-PrimInt-for-u64">u64</a><a href="#impl-PrimInt-for-u8">u8</a><a href="#impl-PrimInt-for-usize">usize</a></div><a class="sidebar-title" href="#implementors">Implementors</a></div><p class='location'><a href='../index.html'>num_traits</a>::<wbr><a href='index.html'>int</a></p><script>window.sidebarCurrent = {name: 'PrimInt', ty: 'trait', relpath: ''};</script><script defer src="sidebar-items.js"></script></div></nav><div class="theme-picker"><button id="theme-picker" aria-label="Pick another theme!"><img src="../../brush.svg" width="18" alt="Pick another theme!"></button><div id="theme-choices"></div></div><script src="../../theme.js"></script><nav class="sub"><form class="search-form js-only"><div class="search-container"><div><select id="crate-search"><option value="All crates">All crates</option></select><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"></div><a id="settings-menu" href="../../settings.html"><img src="../../wheel.svg" width="18" alt="Change settings"></a></div></form></nav><section id="main" class="content"><h1 class='fqn'><span class='out-of-band'><span id='render-detail'><a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class='inner'>−</span>]</a></span><a class='srclink' href='../../src/num_traits/int.rs.html#34-307' title='goto source code'>[s
|
|||
|
fn <a href='#tymethod.count_ones' class='fnname'>count_ones</a>(self) -> <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>;
|
|||
|
<div class='item-spacer'></div> fn <a href='#tymethod.count_zeros' class='fnname'>count_zeros</a>(self) -> <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>;
|
|||
|
<div class='item-spacer'></div> fn <a href='#tymethod.leading_zeros' class='fnname'>leading_zeros</a>(self) -> <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>;
|
|||
|
<div class='item-spacer'></div> fn <a href='#tymethod.trailing_zeros' class='fnname'>trailing_zeros</a>(self) -> <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>;
|
|||
|
<div class='item-spacer'></div> fn <a href='#tymethod.rotate_left' class='fnname'>rotate_left</a>(self, n: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>) -> Self;
|
|||
|
<div class='item-spacer'></div> fn <a href='#tymethod.rotate_right' class='fnname'>rotate_right</a>(self, n: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>) -> Self;
|
|||
|
<div class='item-spacer'></div> fn <a href='#tymethod.signed_shl' class='fnname'>signed_shl</a>(self, n: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>) -> Self;
|
|||
|
<div class='item-spacer'></div> fn <a href='#tymethod.signed_shr' class='fnname'>signed_shr</a>(self, n: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>) -> Self;
|
|||
|
<div class='item-spacer'></div> fn <a href='#tymethod.unsigned_shl' class='fnname'>unsigned_shl</a>(self, n: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>) -> Self;
|
|||
|
<div class='item-spacer'></div> fn <a href='#tymethod.unsigned_shr' class='fnname'>unsigned_shr</a>(self, n: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>) -> Self;
|
|||
|
<div class='item-spacer'></div> fn <a href='#tymethod.swap_bytes' class='fnname'>swap_bytes</a>(self) -> Self;
|
|||
|
<div class='item-spacer'></div> fn <a href='#tymethod.from_be' class='fnname'>from_be</a>(x: Self) -> Self;
|
|||
|
<div class='item-spacer'></div> fn <a href='#tymethod.from_le' class='fnname'>from_le</a>(x: Self) -> Self;
|
|||
|
<div class='item-spacer'></div> fn <a href='#tymethod.to_be' class='fnname'>to_be</a>(self) -> Self;
|
|||
|
<div class='item-spacer'></div> fn <a href='#tymethod.to_le' class='fnname'>to_le</a>(self) -> Self;
|
|||
|
<div class='item-spacer'></div> fn <a href='#tymethod.pow' class='fnname'>pow</a>(self, exp: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>) -> Self;
|
|||
|
}</pre></div><div class='docblock'><p>Generic trait for primitive integers.</p>
|
|||
|
<p>The <code>PrimInt</code> trait is an abstraction over the builtin primitive integer types (e.g., <code>u8</code>,
|
|||
|
<code>u32</code>, <code>isize</code>, <code>i128</code>, ...). It inherits the basic numeric traits and extends them with
|
|||
|
bitwise operators and non-wrapping arithmetic.</p>
|
|||
|
<p>The trait explicitly inherits <code>Copy</code>, <code>Eq</code>, <code>Ord</code>, and <code>Sized</code>. The intention is that all
|
|||
|
types implementing this trait behave like primitive types that are passed by value by default
|
|||
|
and behave like builtin integers. Furthermore, the types are expected to expose the integer
|
|||
|
value in binary representation and support bitwise operators. The standard bitwise operations
|
|||
|
(e.g., bitwise-and, bitwise-or, right-shift, left-shift) are inherited and the trait extends
|
|||
|
these with introspective queries (e.g., <code>PrimInt::count_ones()</code>, <code>PrimInt::leading_zeros()</code>),
|
|||
|
bitwise combinators (e.g., <code>PrimInt::rotate_left()</code>), and endianness converters (e.g.,
|
|||
|
<code>PrimInt::to_be()</code>).</p>
|
|||
|
<p>All <code>PrimInt</code> types are expected to be fixed-width binary integers. The width can be queried
|
|||
|
via <code>T::zero().count_zeros()</code>. The trait currently lacks a way to query the width at
|
|||
|
compile-time.</p>
|
|||
|
<p>While a default implementation for all builtin primitive integers is provided, the trait is in
|
|||
|
no way restricted to these. Other integer types that fulfil the requirements are free to
|
|||
|
implement the trait was well.</p>
|
|||
|
<p>This trait and many of the method names originate in the unstable <code>core::num::Int</code> trait from
|
|||
|
the rust standard library. The original trait was never stabilized and thus removed from the
|
|||
|
standard library.</p>
|
|||
|
</div>
|
|||
|
<h2 id='required-methods' class='small-section-header'>Required methods<a href='#required-methods' class='anchor'></a></h2><div class='methods'><h3 id='tymethod.count_ones' class='method'><code id='count_ones.v'>fn <a href='#tymethod.count_ones' class='fnname'>count_ones</a>(self) -> <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a></code></h3><div class='docblock'><p>Returns the number of ones in the binary representation of <code>self</code>.</p>
|
|||
|
<h1 id="examples" class="section-header"><a href="#examples">Examples</a></h1>
|
|||
|
<div class="example-wrap"><pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">use</span> <span class="ident">num_traits</span>::<span class="ident">PrimInt</span>;
|
|||
|
|
|||
|
<span class="kw">let</span> <span class="ident">n</span> <span class="op">=</span> <span class="number">0b01001100u8</span>;
|
|||
|
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">n</span>.<span class="ident">count_ones</span>(), <span class="number">3</span>);</pre></div>
|
|||
|
</div><h3 id='tymethod.count_zeros' class='method'><code id='count_zeros.v'>fn <a href='#tymethod.count_zeros' class='fnname'>count_zeros</a>(self) -> <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a></code></h3><div class='docblock'><p>Returns the number of zeros in the binary representation of <code>self</code>.</p>
|
|||
|
<h1 id="examples-1" class="section-header"><a href="#examples-1">Examples</a></h1>
|
|||
|
<div class="example-wrap"><pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">use</span> <span class="ident">num_traits</span>::<span class="ident">PrimInt</span>;
|
|||
|
|
|||
|
<span class="kw">let</span> <span class="ident">n</span> <span class="op">=</span> <span class="number">0b01001100u8</span>;
|
|||
|
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">n</span>.<span class="ident">count_zeros</span>(), <span class="number">5</span>);</pre></div>
|
|||
|
</div><h3 id='tymethod.leading_zeros' class='method'><code id='leading_zeros.v'>fn <a href='#tymethod.leading_zeros' class='fnname'>leading_zeros</a>(self) -> <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a></code></h3><div class='docblock'><p>Returns the number of leading zeros in the binary representation
|
|||
|
of <code>self</code>.</p>
|
|||
|
<h1 id="examples-2" class="section-header"><a href="#examples-2">Examples</a></h1>
|
|||
|
<div class="example-wrap"><pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">use</span> <span class="ident">num_traits</span>::<span class="ident">PrimInt</span>;
|
|||
|
|
|||
|
<span class="kw">let</span> <span class="ident">n</span> <span class="op">=</span> <span class="number">0b0101000u16</span>;
|
|||
|
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">n</span>.<span class="ident">leading_zeros</span>(), <span class="number">10</span>);</pre></div>
|
|||
|
</div><h3 id='tymethod.trailing_zeros' class='method'><code id='trailing_zeros.v'>fn <a href='#tymethod.trailing_zeros' class='fnname'>trailing_zeros</a>(self) -> <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a></code></h3><div class='docblock'><p>Returns the number of trailing zeros in the binary representation
|
|||
|
of <code>self</code>.</p>
|
|||
|
<h1 id="examples-3" class="section-header"><a href="#examples-3">Examples</a></h1>
|
|||
|
<div class="example-wrap"><pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">use</span> <span class="ident">num_traits</span>::<span class="ident">PrimInt</span>;
|
|||
|
|
|||
|
<span class="kw">let</span> <span class="ident">n</span> <span class="op">=</span> <span class="number">0b0101000u16</span>;
|
|||
|
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">n</span>.<span class="ident">trailing_zeros</span>(), <span class="number">3</span>);</pre></div>
|
|||
|
</div><h3 id='tymethod.rotate_left' class='method'><code id='rotate_left.v'>fn <a href='#tymethod.rotate_left' class='fnname'>rotate_left</a>(self, n: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>) -> Self</code></h3><div class='docblock'><p>Shifts the bits to the left by a specified amount amount, <code>n</code>, wrapping
|
|||
|
the truncated bits to the end of the resulting integer.</p>
|
|||
|
<h1 id="examples-4" class="section-header"><a href="#examples-4">Examples</a></h1>
|
|||
|
<div class="example-wrap"><pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">use</span> <span class="ident">num_traits</span>::<span class="ident">PrimInt</span>;
|
|||
|
|
|||
|
<span class="kw">let</span> <span class="ident">n</span> <span class="op">=</span> <span class="number">0x0123456789ABCDEFu64</span>;
|
|||
|
<span class="kw">let</span> <span class="ident">m</span> <span class="op">=</span> <span class="number">0x3456789ABCDEF012u64</span>;
|
|||
|
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">n</span>.<span class="ident">rotate_left</span>(<span class="number">12</span>), <span class="ident">m</span>);</pre></div>
|
|||
|
</div><h3 id='tymethod.rotate_right' class='method'><code id='rotate_right.v'>fn <a href='#tymethod.rotate_right' class='fnname'>rotate_right</a>(self, n: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>) -> Self</code></h3><div class='docblock'><p>Shifts the bits to the right by a specified amount amount, <code>n</code>, wrapping
|
|||
|
the truncated bits to the beginning of the resulting integer.</p>
|
|||
|
<h1 id="examples-5" class="section-header"><a href="#examples-5">Examples</a></h1>
|
|||
|
<div class="example-wrap"><pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">use</span> <span class="ident">num_traits</span>::<span class="ident">PrimInt</span>;
|
|||
|
|
|||
|
<span class="kw">let</span> <span class="ident">n</span> <span class="op">=</span> <span class="number">0x0123456789ABCDEFu64</span>;
|
|||
|
<span class="kw">let</span> <span class="ident">m</span> <span class="op">=</span> <span class="number">0xDEF0123456789ABCu64</span>;
|
|||
|
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">n</span>.<span class="ident">rotate_right</span>(<span class="number">12</span>), <span class="ident">m</span>);</pre></div>
|
|||
|
</div><h3 id='tymethod.signed_shl' class='method'><code id='signed_shl.v'>fn <a href='#tymethod.signed_shl' class='fnname'>signed_shl</a>(self, n: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>) -> Self</code></h3><div class='docblock'><p>Shifts the bits to the left by a specified amount amount, <code>n</code>, filling
|
|||
|
zeros in the least significant bits.</p>
|
|||
|
<p>This is bitwise equivalent to signed <code>Shl</code>.</p>
|
|||
|
<h1 id="examples-6" class="section-header"><a href="#examples-6">Examples</a></h1>
|
|||
|
<div class="example-wrap"><pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">use</span> <span class="ident">num_traits</span>::<span class="ident">PrimInt</span>;
|
|||
|
|
|||
|
<span class="kw">let</span> <span class="ident">n</span> <span class="op">=</span> <span class="number">0x0123456789ABCDEFu64</span>;
|
|||
|
<span class="kw">let</span> <span class="ident">m</span> <span class="op">=</span> <span class="number">0x3456789ABCDEF000u64</span>;
|
|||
|
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">n</span>.<span class="ident">signed_shl</span>(<span class="number">12</span>), <span class="ident">m</span>);</pre></div>
|
|||
|
</div><h3 id='tymethod.signed_shr' class='method'><code id='signed_shr.v'>fn <a href='#tymethod.signed_shr' class='fnname'>signed_shr</a>(self, n: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>) -> Self</code></h3><div class='docblock'><p>Shifts the bits to the right by a specified amount amount, <code>n</code>, copying
|
|||
|
the "sign bit" in the most significant bits even for unsigned types.</p>
|
|||
|
<p>This is bitwise equivalent to signed <code>Shr</code>.</p>
|
|||
|
<h1 id="examples-7" class="section-header"><a href="#examples-7">Examples</a></h1>
|
|||
|
<div class="example-wrap"><pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">use</span> <span class="ident">num_traits</span>::<span class="ident">PrimInt</span>;
|
|||
|
|
|||
|
<span class="kw">let</span> <span class="ident">n</span> <span class="op">=</span> <span class="number">0xFEDCBA9876543210u64</span>;
|
|||
|
<span class="kw">let</span> <span class="ident">m</span> <span class="op">=</span> <span class="number">0xFFFFEDCBA9876543u64</span>;
|
|||
|
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">n</span>.<span class="ident">signed_shr</span>(<span class="number">12</span>), <span class="ident">m</span>);</pre></div>
|
|||
|
</div><h3 id='tymethod.unsigned_shl' class='method'><code id='unsigned_shl.v'>fn <a href='#tymethod.unsigned_shl' class='fnname'>unsigned_shl</a>(self, n: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>) -> Self</code></h3><div class='docblock'><p>Shifts the bits to the left by a specified amount amount, <code>n</code>, filling
|
|||
|
zeros in the least significant bits.</p>
|
|||
|
<p>This is bitwise equivalent to unsigned <code>Shl</code>.</p>
|
|||
|
<h1 id="examples-8" class="section-header"><a href="#examples-8">Examples</a></h1>
|
|||
|
<div class="example-wrap"><pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">use</span> <span class="ident">num_traits</span>::<span class="ident">PrimInt</span>;
|
|||
|
|
|||
|
<span class="kw">let</span> <span class="ident">n</span> <span class="op">=</span> <span class="number">0x0123456789ABCDEFi64</span>;
|
|||
|
<span class="kw">let</span> <span class="ident">m</span> <span class="op">=</span> <span class="number">0x3456789ABCDEF000i64</span>;
|
|||
|
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">n</span>.<span class="ident">unsigned_shl</span>(<span class="number">12</span>), <span class="ident">m</span>);</pre></div>
|
|||
|
</div><h3 id='tymethod.unsigned_shr' class='method'><code id='unsigned_shr.v'>fn <a href='#tymethod.unsigned_shr' class='fnname'>unsigned_shr</a>(self, n: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>) -> Self</code></h3><div class='docblock'><p>Shifts the bits to the right by a specified amount amount, <code>n</code>, filling
|
|||
|
zeros in the most significant bits.</p>
|
|||
|
<p>This is bitwise equivalent to unsigned <code>Shr</code>.</p>
|
|||
|
<h1 id="examples-9" class="section-header"><a href="#examples-9">Examples</a></h1>
|
|||
|
<div class="example-wrap"><pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">use</span> <span class="ident">num_traits</span>::<span class="ident">PrimInt</span>;
|
|||
|
|
|||
|
<span class="kw">let</span> <span class="ident">n</span> <span class="op">=</span> <span class="op">-</span><span class="number">8i8</span>; <span class="comment">// 0b11111000</span>
|
|||
|
<span class="kw">let</span> <span class="ident">m</span> <span class="op">=</span> <span class="number">62i8</span>; <span class="comment">// 0b00111110</span>
|
|||
|
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">n</span>.<span class="ident">unsigned_shr</span>(<span class="number">2</span>), <span class="ident">m</span>);</pre></div>
|
|||
|
</div><h3 id='tymethod.swap_bytes' class='method'><code id='swap_bytes.v'>fn <a href='#tymethod.swap_bytes' class='fnname'>swap_bytes</a>(self) -> Self</code></h3><div class='docblock'><p>Reverses the byte order of the integer.</p>
|
|||
|
<h1 id="examples-10" class="section-header"><a href="#examples-10">Examples</a></h1>
|
|||
|
<div class="example-wrap"><pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">use</span> <span class="ident">num_traits</span>::<span class="ident">PrimInt</span>;
|
|||
|
|
|||
|
<span class="kw">let</span> <span class="ident">n</span> <span class="op">=</span> <span class="number">0x0123456789ABCDEFu64</span>;
|
|||
|
<span class="kw">let</span> <span class="ident">m</span> <span class="op">=</span> <span class="number">0xEFCDAB8967452301u64</span>;
|
|||
|
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">n</span>.<span class="ident">swap_bytes</span>(), <span class="ident">m</span>);</pre></div>
|
|||
|
</div><h3 id='tymethod.from_be' class='method'><code id='from_be.v'>fn <a href='#tymethod.from_be' class='fnname'>from_be</a>(x: Self) -> Self</code></h3><div class='docblock'><p>Convert an integer from big endian to the target's endianness.</p>
|
|||
|
<p>On big endian this is a no-op. On little endian the bytes are swapped.</p>
|
|||
|
<h1 id="examples-11" class="section-header"><a href="#examples-11">Examples</a></h1>
|
|||
|
<div class="example-wrap"><pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">use</span> <span class="ident">num_traits</span>::<span class="ident">PrimInt</span>;
|
|||
|
|
|||
|
<span class="kw">let</span> <span class="ident">n</span> <span class="op">=</span> <span class="number">0x0123456789ABCDEFu64</span>;
|
|||
|
|
|||
|
<span class="kw">if</span> <span class="macro">cfg</span><span class="macro">!</span>(<span class="ident">target_endian</span> <span class="op">=</span> <span class="string">"big"</span>) {
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">u64</span>::<span class="ident">from_be</span>(<span class="ident">n</span>), <span class="ident">n</span>)
|
|||
|
} <span class="kw">else</span> {
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">u64</span>::<span class="ident">from_be</span>(<span class="ident">n</span>), <span class="ident">n</span>.<span class="ident">swap_bytes</span>())
|
|||
|
}</pre></div>
|
|||
|
</div><h3 id='tymethod.from_le' class='method'><code id='from_le.v'>fn <a href='#tymethod.from_le' class='fnname'>from_le</a>(x: Self) -> Self</code></h3><div class='docblock'><p>Convert an integer from little endian to the target's endianness.</p>
|
|||
|
<p>On little endian this is a no-op. On big endian the bytes are swapped.</p>
|
|||
|
<h1 id="examples-12" class="section-header"><a href="#examples-12">Examples</a></h1>
|
|||
|
<div class="example-wrap"><pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">use</span> <span class="ident">num_traits</span>::<span class="ident">PrimInt</span>;
|
|||
|
|
|||
|
<span class="kw">let</span> <span class="ident">n</span> <span class="op">=</span> <span class="number">0x0123456789ABCDEFu64</span>;
|
|||
|
|
|||
|
<span class="kw">if</span> <span class="macro">cfg</span><span class="macro">!</span>(<span class="ident">target_endian</span> <span class="op">=</span> <span class="string">"little"</span>) {
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">u64</span>::<span class="ident">from_le</span>(<span class="ident">n</span>), <span class="ident">n</span>)
|
|||
|
} <span class="kw">else</span> {
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">u64</span>::<span class="ident">from_le</span>(<span class="ident">n</span>), <span class="ident">n</span>.<span class="ident">swap_bytes</span>())
|
|||
|
}</pre></div>
|
|||
|
</div><h3 id='tymethod.to_be' class='method'><code id='to_be.v'>fn <a href='#tymethod.to_be' class='fnname'>to_be</a>(self) -> Self</code></h3><div class='docblock'><p>Convert <code>self</code> to big endian from the target's endianness.</p>
|
|||
|
<p>On big endian this is a no-op. On little endian the bytes are swapped.</p>
|
|||
|
<h1 id="examples-13" class="section-header"><a href="#examples-13">Examples</a></h1>
|
|||
|
<div class="example-wrap"><pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">use</span> <span class="ident">num_traits</span>::<span class="ident">PrimInt</span>;
|
|||
|
|
|||
|
<span class="kw">let</span> <span class="ident">n</span> <span class="op">=</span> <span class="number">0x0123456789ABCDEFu64</span>;
|
|||
|
|
|||
|
<span class="kw">if</span> <span class="macro">cfg</span><span class="macro">!</span>(<span class="ident">target_endian</span> <span class="op">=</span> <span class="string">"big"</span>) {
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">n</span>.<span class="ident">to_be</span>(), <span class="ident">n</span>)
|
|||
|
} <span class="kw">else</span> {
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">n</span>.<span class="ident">to_be</span>(), <span class="ident">n</span>.<span class="ident">swap_bytes</span>())
|
|||
|
}</pre></div>
|
|||
|
</div><h3 id='tymethod.to_le' class='method'><code id='to_le.v'>fn <a href='#tymethod.to_le' class='fnname'>to_le</a>(self) -> Self</code></h3><div class='docblock'><p>Convert <code>self</code> to little endian from the target's endianness.</p>
|
|||
|
<p>On little endian this is a no-op. On big endian the bytes are swapped.</p>
|
|||
|
<h1 id="examples-14" class="section-header"><a href="#examples-14">Examples</a></h1>
|
|||
|
<div class="example-wrap"><pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">use</span> <span class="ident">num_traits</span>::<span class="ident">PrimInt</span>;
|
|||
|
|
|||
|
<span class="kw">let</span> <span class="ident">n</span> <span class="op">=</span> <span class="number">0x0123456789ABCDEFu64</span>;
|
|||
|
|
|||
|
<span class="kw">if</span> <span class="macro">cfg</span><span class="macro">!</span>(<span class="ident">target_endian</span> <span class="op">=</span> <span class="string">"little"</span>) {
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">n</span>.<span class="ident">to_le</span>(), <span class="ident">n</span>)
|
|||
|
} <span class="kw">else</span> {
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">n</span>.<span class="ident">to_le</span>(), <span class="ident">n</span>.<span class="ident">swap_bytes</span>())
|
|||
|
}</pre></div>
|
|||
|
</div><h3 id='tymethod.pow' class='method'><code id='pow.v'>fn <a href='#tymethod.pow' class='fnname'>pow</a>(self, exp: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>) -> Self</code></h3><div class='docblock'><p>Raises self to the power of <code>exp</code>, using exponentiation by squaring.</p>
|
|||
|
<h1 id="examples-15" class="section-header"><a href="#examples-15">Examples</a></h1>
|
|||
|
<div class="example-wrap"><pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">use</span> <span class="ident">num_traits</span>::<span class="ident">PrimInt</span>;
|
|||
|
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="number">2i32</span>.<span class="ident">pow</span>(<span class="number">4</span>), <span class="number">16</span>);</pre></div>
|
|||
|
</div></div><span class='loading-content'>Loading content...</span>
|
|||
|
<h2 id='foreign-impls' class='small-section-header'>Implementations on Foreign Types<a href='#foreign-impls' class='anchor'></a></h2><h3 id='impl-PrimInt-for-u8' class='impl'><code class='in-band'>impl <a class="trait" href="../../num_traits/int/trait.PrimInt.html" title="trait num_traits::int::PrimInt">PrimInt</a> for <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u8.html">u8</a></code><a href='#impl-PrimInt-for-u8' class='anchor'></a><a class='srclink' href='../../src/num_traits/int.rs.html#311-391' title='goto source code'>[src]</a></h3><div class='impl-items'><h4 id='method.count_ones' class="method hidden"><code id='count_ones.v-1'>fn <a href='#method.count_ones' class='fnname'>count_ones</a>(self) -> <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a></code><a class='srclink' href='../../src/num_traits/int.rs.html#313-315' title='goto source code'>[src]</a></h4><h4 id='method.count_zeros' class="method hidden"><code id='count_zeros.v-1'>fn <a href='#method.count_zeros' class='fnname'>count_zeros</a>(self) -> <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a></code><a class='srclink' href='../../src/num_traits/int.rs.html#318-320' title='goto source code'>[src]</a></h4><h4 id='method.leading_zeros' class="method hidden"><code id='leading_zeros.v-1'>fn <a href='#method.leading_zeros' class='fnname'>leading_zeros</a>(self) -> <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a></code><a class='srclink' href='../../src/num_traits/int.rs.html#323-325' title='goto source code'>[src]</a></h4><h4 id='method.trailing_zeros' class="method hidden"><code id='trailing_zeros.v-1'>fn <a href='#method.trailing_zeros' class='fnname'>trailing_zeros</a>(self) -> <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a></code><a class='srclink' href='../../src/num_traits/int.rs.html#328-330' title='goto source code'>[src]</a></h4><h4 id='method.rotate_left' class="method hidden"><code id='rotate_left.v-1'>fn <a href='#method.rotate_left' class='fnname'>rotate_left</a>(self, n: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>) -> Self</code><a class='srclink' href='../../src/num_traits/int.rs.html#333-335' title='goto source code'>[src]</a></h4><h4 id='method.rotate_right' class="method hidden"><code id='rotate_right.v-1'>fn <a href='#method.rotate_right' class='fnname'>rotate_right</a>(self, n: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>) -> Self</code><a class='srclink' href='../../src/num_traits/int.rs.html#338-340' title='goto source code'>[src]</a></h4><h4 id='method.signed_shl' class="method hidden"><code id='signed_shl.v-1'>fn <a href='#method.signed_shl' class='fnname'>signed_shl</a>(self, n: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>) -> Self</code><a class='srclink' href='../../src/num_traits/int.rs.html#343-345' title='goto source code'>[src]</a></h4><h4 id='method.signed_shr' class="method hidden"><code id='signed_shr.v-1'>fn <a href='#method.signed_shr' class='fnname'>signed_shr</a>(self, n: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>) -> Self</code><a class='srclink' href='../../src/num_traits/int.rs.html#348-350' title='goto source code'>[src]</a></h4><h4 id='method.unsigned_shl' class="method hidden"><code id='unsigned_shl.v-1'>fn <a href='#method.unsigned_shl' class='fnname'>unsigned_shl</a>(self, n: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>) -> Self</code><a class='srclink' href='../../src/num_traits/int.rs.html#353-355' title='goto source code'>[src]</a></h4><h4 id='method.unsigned_shr' class="method hidden"><code id='unsigned_shr.v-1'>fn <a href='#method.unsigned_shr' class='fnname'>unsigned_shr</a>(self, n: <a class="primitive" href="https://doc.rust-lang
|
|||
|
<h2 id='implementors' class='small-section-header'>Implementors<a href='#implementors' class='anchor'></a></h2><div class='item-list' id='implementors-list'></div><span class='loading-content'>Loading content...</span><script type="text/javascript">window.inlined_types=new Set([]);</script><script type="text/javascript" async
|
|||
|
src="../../implementors/num_traits/int/trait.PrimInt.js">
|
|||
|
</script></section><section id="search" class="content hidden"></section><section class="footer"></section><aside id="help" class="hidden"><div><h1 class="hidden">Help</h1><div class="shortcuts"><h2>Keyboard Shortcuts</h2><dl><dt><kbd>?</kbd></dt><dd>Show this help dialog</dd><dt><kbd>S</kbd></dt><dd>Focus the search field</dd><dt><kbd>↑</kbd></dt><dd>Move up in search results</dd><dt><kbd>↓</kbd></dt><dd>Move down in search results</dd><dt><kbd>↹</kbd></dt><dd>Switch tab</dd><dt><kbd>⏎</kbd></dt><dd>Go to active search result</dd><dt><kbd>+</kbd></dt><dd>Expand all sections</dd><dt><kbd>-</kbd></dt><dd>Collapse all sections</dd></dl></div><div class="infos"><h2>Search Tricks</h2><p>Prefix searches with a type followed by a colon (e.g., <code>fn:</code>) to restrict the search to a given type.</p><p>Accepted types are: <code>fn</code>, <code>mod</code>, <code>struct</code>, <code>enum</code>, <code>trait</code>, <code>type</code>, <code>macro</code>, and <code>const</code>.</p><p>Search functions by type signature (e.g., <code>vec -> usize</code> or <code>* -> vec</code>)</p><p>Search multiple things at once by splitting your query with comma (e.g., <code>str,u8</code> or <code>String,struct:Vec,test</code>)</p></div></div></aside><script>window.rootPath = "../../";window.currentCrate = "num_traits";</script><script src="../../aliases.js"></script><script src="../../main.js"></script><script defer src="../../search-index.js"></script></body></html>
|