Libraries
Rust
bit-byte-bit
Implements an n-bit array as ⌈n/8⌉ bytes.
Usage
Instantiation
Constructors defined in the [Bits] implementation generate bit strings
with a length that is either input-defined, or in the case of [Bits::new],
8 times the length of the iterator.
Constructors defined by the From or
FromIterator traits generate bit strings with a length
based on the input. If a collection of bool, the length will be equal to
that of the input. If the input is a collection of u8, the length counts
all bits up to and including the most significant one bit.
#![allow(unused)] fn main() { use bit_byte_bit::Bits; let bits = Bits::new([0x0A, 0x0B, 0x0C]); assert_eq!(bits.len(), 24); let bits_no_lz = Bits::from([0x0A, 0x0B, 0x0C]); assert_eq!(bits_no_lz.len(), 20); let aligned_bits = Bits::aligned(16, [0x0A, 0x0B, 0x0C]); assert_eq!(aligned_bits.len(), 32); let packed_bits = Bits::packed([0x0A, 0x0B, 0x0C]); assert_eq!(packed_bits, Bits::from([0xBA, 0x0C])); let bools = Bits::from([true, false, true, false, false, true]); assert_eq!(bools, Bits::slice(&[0x25], 6)); let slice = Bits::take(&[0x0A, 0x0B, 0x0C], 3, 9); assert_eq!(slice, Bits::from([0x61, 0x01])); }
The bits! macro is also provided for convenience, wrapping the constructors:
Bits::newBits::alignedBits::onesBits::packedBits::zeros
#![allow(unused)] fn main() { use bit_byte_bit::{Bits, bits}; let bits = bits![0x0A, 0x0B, 0x0C]; assert_eq!(bits, Bits::new([0x0A, 0x0B, 0x0C])); let repeated_bytes = bits![8; 0x0F; 20]; assert_eq!(repeated_bytes, Bits::new([0x0F; 20])); let aligned_bits = bits![0x0A, 0x0B, 0x0C; => 16]; assert_eq!(aligned_bits.len(), 32); let ones = bits![1; 20]; assert_eq!(ones, Bits::ones(20)); let packed_bits = bits![0x0A, 0x0B, 0x0C; %]; assert_eq!(packed_bits, Bits::from([0xBA, 0x0C])); let zeros = bits![0; 20]; assert_eq!(zeros, Bits::zeros(20)); }
Indexing
Bits::i can be used to access an individual bit. Bits can be set to one,
reset to zero, or toggled.
#![allow(unused)] fn main() { use bit_byte_bit::{Bits}; let mut bits = Bits::new([0x0A, 0x0B, 0x0C]); assert_eq!(bits.i(0), 0); assert_eq!(bits.i(1), 1); bits.set(0); bits.reset(1); assert_eq!(bits.i(0), 1); assert_eq!(bits.i(1), 0); bits.toggle(0); bits.toggle(1); assert_eq!(bits.i(0), 0); assert_eq!(bits.i(1), 1); }
Bitwise Operations
Bit strings support
- complement
- and
- or
- xor
- left and right shifting
- left and right rotation
- reversal
- counting ones or zeros
- counting leading ones or zeros
- counting trailing ones or zeros
Each of complement, and, or, and xor can be done in-place by calling the *_mut version of the method.
Reversal, shifting, and rotation can be done in-place using the verb form of the method, as opposed to the
adjective form that is, reverse versus reversed, or rotate_left versus rotated_left.
Also note that the method for counting ones is called hamming_weight.
ⓘ For methods ending in either
_leftor_right, bit 0 is considered the most right bit, while bit n-1 is the most left.
#![allow(unused)] fn main() { use bit_byte_bit::{Bits}; let x = Bits::new([0x20, 0x30, 0x40]); let y = Bits::new([0xA0, 0xB0, 0xC0]); assert_eq!(x.and(&y), Bits::new([0x20, 0x30, 0x40])); assert_eq!(x.complement(), Bits::new([0xDF, 0xCF, 0xBF])); assert_eq!(x.or(&y), Bits::new([0xA0, 0xB0, 0xC0])); assert_eq!(x.xor(&y), Bits::new([0x80, 0x80, 0x80])); let bits = Bits::from([0x0A, 0x0B, 0x0C]); assert_eq!(bits.len(), 20); assert_eq!(bits.shifted_left(17), Bits::slice(&[0x00, 0x00, 0x04], 20)); assert_eq!(bits.shifted_right(17), Bits::slice(&[0x06, 0x00, 0x00], 20)); assert_eq!(bits.rotated_left(4), Bits::slice(&[0xAC, 0xB0, 0x00], 20)); assert_eq!(bits.rotated_right(4), Bits::slice(&[0xB0, 0xC0, 0x0A], 20)); }
Iteration
Iteration can be done in several ways.
Bits::iter-- iterates over individual bitsBits::iter_from-- iterates over individual bits starting from a given index in the bit stringBits::into_iter-- iterates over individual bits, consuming bit stringBits::bytes(via&[u8]::iter) -- iterates over underlying bytesBits::into_bytes-- iterates over underlying bytes, consuming bit stringVec::<*>::from-- converts a bit string into a vector, consuming the bit string.*is a floating point or integer type that is notisizeorusize,i128,u128.bits_as::vec_*-- same asVec::<*>::frombut does not consume the bit string.
#![allow(unused)] fn main() { use bit_byte_bit::{Bits, bits_as}; let bits = Bits::new([0xBA, 0xDC, 0xFE]); let mut ones = 0; for bit in bits.iter() { if bit == 1 { ones += 1; } } assert_eq!(ones, 17); ones = 0; for bit in bits.iter_from(13) { if bit == 1 { ones += 1; } } assert_eq!(ones, 9); let bytes_iter = bits.bytes().iter(); assert_eq!(bytes_iter.next(), Some(&0xAB)); let shorts = bits_as::vec_i16(&bits); let mut shorts_iter = shorts.into_iter(); assert_eq!(shorts_iter.next(), Some(0xDCBAu16 as i16)); assert_eq!(shorts_iter.next(), Some(0xFFFEu16 as i16)); let ushorts = Vec::<u16>::from(bits); let mut ushorts_iter = ushorts.into_iter(); assert_eq!(ushorts_iter.next(), Some(0xDCBA)); assert_eq!(ushorts_iter.next(), Some(0x00FE)); }
Conversion
Bit strings can be converted using one of the From<Bits>
implementations or the bits_as module. This module implements the trait methods as non-consuming methods.
Example
#![allow(unused)] fn main() { use bit_byte_bit::{Bits, bits_as}; let bits = Bits::new([0x10, 0x32, 0x54, 0x76, 0x89, 0xBA, 0xDC, 0xFE]); let signed_ints_1 = bits_as::vec_i32(&bits); let signed_ints_2 = Vec::<i32>::from(bits); // `bits` is moved. assert_eq!(signed_ints_1, signed_ints_2); let bits = Bits::new([0x10, 0x32, 0x54, 0x76, 0x89, 0xBA, 0xDC, 0xFE]); let signed_int_1 = bits_as::int64(&bits); let float64 = bits_as::float64(&bits); let signed_int_2 = i64::from(bits); // `bits` is moved. assert_eq!(signed_int_1, signed_int_2); assert_eq!(float64, f64::from_bits(0xFEDCBA9876543210)); }
Bit Schemes
Scheme implements a simple bit string pattern matcher inspired by Erlang's bit syntax. Schemes are used to give meaning to different regions of a bit string.
Example
ipv4_header_scheme will define the part of the IPv4 datagram header that does not include the options.
This scheme will then be used to process more bits from the bit string. Scheme::split_* methods return a vector
of Bits corresponding to each field. Scheme::extract_from_* methods return a hashmap, corresponding each
field with a key.
#![allow(unused)] fn main() { use bit_byte_bit::{Bits, bits_as, Field, Scheme}; const IPV4_HEADER_BIT_LEN: usize = 160; const IPV4_HEADER_BYTE_LEN: usize = 20; const IPV4_HEADER_KEYS: [&str; 13] = [ "Version", "IHL", "DSCP", "ECN", "Total Length", "Identification", "Flags", "Fragment Offset", "TTL", "Protocol", "Header Checksum", "Source Address", "Destination Address" ]; let ipv4_header_scheme: Scheme = Scheme::new([ Field::UNSIGNED_4, // Version Field::UNSIGNED_4, // Internet Header Length Field::unsigned(6), // Differentiated Services Code Point Field::UNSIGNED_2, // Explicit Congestion Notification Field::UNSIGNED_16, // Total Length Field::UNSIGNED_16, // Identification Field::unsigned(3), // Flags Field::unsigned(13), // Fragment Offset Field::UNSIGNED_8, // Time To Live Field::UNSIGNED_8, // Protocol Field::UNSIGNED_16, // Header Checksum Field::UNSIGNED_32, // Source Address Field::UNSIGNED_32 // Destination Address ]); let bits = Bits::new([ 0x54, 0x00, 0x34, 0x00, 0x16, 0x41, 0x02, 0x00, 128, 6, 0, 0, 192, 168, 5, 45, 91, 198, 174, 192 ]); let mut data = ipv4_header_scheme.extract_from_bits(&bits, &IPV4_HEADER_KEYS); let total_length = bits_as::unsigned(&data[&"Total Length"]); let header_32_bit_len = bits_as::unsigned(&data[&"IHL"]); let options_byte_len = (header_32_bit_len - 5) * 4; let options_bit_len = options_byte_len * 8; let options = Bits::take(bits.bytes(), IPV4_HEADER_BIT_LEN, options_bit_len); let payload_start = IPV4_HEADER_BIT_LEN + options_bit_len; let payload_bit_len = (total_length - IPV4_HEADER_BYTE_LEN - options_byte_len) * 8; let payload = Bits::take(bits.bytes(), IPV4_HEADER_BIT_LEN + options_bit_len, payload_bit_len); assert_eq!(bits_as::uint8(&data[&"Version"]), 4); assert_eq!(bits_as::uint8(&data[&"IHL"]), 5); assert_eq!(bits_as::uint8(&data[&"DSCP"]), 0); assert_eq!(bits_as::uint8(&data[&"ECN"]), 0); assert_eq!(bits_as::uint16(&data[&"Total Length"]), 52); assert_eq!(bits_as::uint16(&data[&"Identification"]), 0x4116); assert_eq!(bits_as::uint8(&data[&"Flags"]), 2); assert_eq!(bits_as::uint16(&data[&"Fragment Offset"]), 0); assert_eq!(bits_as::uint8(&data[&"TTL"]), 128); assert_eq!(bits_as::uint8(&data[&"Protocol"]), 6); assert_eq!(bits_as::uint16(&data[&"Header Checksum"]), 0); assert_eq!(bits_as::vec_u8(&data[&"Source Address"]), vec![192, 168, 5, 45]); assert_eq!(bits_as::vec_u8(&data[&"Destination Address"]), vec![91, 198, 174, 192]); assert_eq!(options, Bits::empty()); assert_eq!(payload, Bits::zeros(8 * 32)); }
Reference
Macros
Structs
bits!
#![allow(unused)] fn main() { macro_rules! bits { () => { ... }; (0; $n:expr) => { ... }; (1; $n:expr) => { ... }; (8; $byte:expr; $n:expr) => { ... }; ($($byte:expr),+ $(,)?) => { ... }; ($($byte:expr),+; => $n:expr) => { ... }; ($($byte:expr),+; %) => { ... }; } }
Macro implementing a subset of the Bits constructors
#![allow(unused)] fn main() { let x1 = bits![]; assert_eq!(x1, Bits::empty()); let x2 = bits![0x0A, 0x0B, 0x0C]; assert_eq!(x2, Bits::new([0x0A, 0x0B, 0x0C])); let x3 = bits![0x0A, 0x0B, 0x0C; => 16]; assert_eq!(x3, Bits::aligned(16, [0x0A, 0x0B, 0x0C])); let x4 = bits![0x0A, 0x0B, 0x0C; %]; assert_eq!(x4, Bits::packed([0x0A, 0x0B, 0x0C])); let x5 = bits![1; 17]; assert_eq!(x5, Bits::ones(17)); let x6 = bits![0; 17]; assert_eq!(x6, Bits::zeros(17)); assert_eq!(x6.len(), 17); let x7 = bits![8; 0; 17]; assert_eq!(x7, Bits::new(vec![0; 17])); assert_eq!(x7.len(), 136); }
bits_as
Module implementing conversions from bits to other types.
Module
#![allow(unused)] fn main() { pub fn float32(bits: &Bits) -> f32 }Converts up to the first 32 bits of a bit string to a 32 bit float
#![allow(unused)] fn main() { use bit_byte_bit::{bits_as, Bits}; let bits = Bits::new([0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE]); let float = bits_as::float32(&bits); assert_eq!(float, f32::from_bits(0x76543210)); let bits = Bits::new([0x10, 0x32, 0x54]); let float = bits_as::float32(&bits); assert_eq!(float, f32::from_bits(0x00543210)); }
#![allow(unused)] fn main() { pub fn float64(bits: &Bits) -> f64 }Converts up to the first 64 bits of a bit string to a 64 bit float
#![allow(unused)] fn main() { use bit_byte_bit::{bits_as, Bits}; let bits = Bits::new([0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE]); let float = bits_as::float64(&bits); assert_eq!(float, f64::from_bits(0xFEDCBA9876543210)); let bits = Bits::new([0x10, 0x32, 0x54]); let float = bits_as::float64(&bits); assert_eq!(float, f64::from_bits(0x0000000000543210)); }
#![allow(unused)] fn main() { pub fn int8(bits: &Bits) -> i8 }Converts up to the first 8 bits of a bit string to 8 bit signed integer.
If the length of the the bit string is less than 8, the result is sign-extended to 8 bits.
#![allow(unused)] fn main() { use bit_byte_bit::{bits_as, Bits}; let bits = Bits::new([0xFE]); let ibyte = bits_as::int8(&bits); assert_eq!(ibyte, 0xFEu8 as i8); let bits = Bits::from([0x5E]); let ibyte = bits_as::int8(&bits); assert_eq!(ibyte, 0xDEu8 as i8); }
#![allow(unused)] fn main() { pub fn int16(bits: &Bits) -> i16 }Converts up the first 16 bits of a bit string to a 16 bit signed integer.
If the length of the the bit string is less than 16, the result is sign-extended to 16 bits.
#![allow(unused)] fn main() { use bit_byte_bit::{bits_as, Bits}; let bits = Bits::new([0x10, 0xFE]); let ishort = bits_as::int16(&bits); assert_eq!(ishort, 0xFE10u16 as i16); let bits = Bits::from([0x10, 0x5E]); let ishort = bits_as::int16(&bits); assert_eq!(ishort, 0xDE10u16 as i16); }
#![allow(unused)] fn main() { pub fn int32(bits: &Bits) -> i32 }Converts up to the first 32 bits of a bit string to a 32 bit signed integer.
If the length of the the bit string is less than 32, the result is sign-extended to 32 bits.
#![allow(unused)] fn main() { use bit_byte_bit::{bits_as, Bits}; let bits = Bits::new([0x10, 0x32, 0x54, 0x76]); let int = bits_as::int32(&bits); assert_eq!(int, 0x76543210); let bits = Bits::from([0x10, 0x32, 0x54, 0x76]); let int = bits_as::int32(&bits); assert_eq!(int, 0xF6543210u32 as i32); }
#![allow(unused)] fn main() { pub fn int64(bits: &Bits) -> i64 }Converts up to the first 64 bits of a bit string to a 64 bit signed integer.
If the length of the the bit string is less than 64, the result is sign-extended to 64 bits.
#![allow(unused)] fn main() { use bit_byte_bit::{bits_as, Bits}; let bits = Bits::new([0x10, 0x32, 0x54, 0x76]); let long = bits_as::int64(&bits); assert_eq!(long, 0x0000000076543210); let bits = Bits::from([0x10, 0x32, 0x54, 0x76]); let long = bits_as::int64(&bits); assert_eq!(long, 0xFFFFFFFFF6543210u64 as i64); }
#![allow(unused)] fn main() { pub fn signed(bits: &Bits) -> isize }Converts up to the first 64 bits of a bit string to a signed integer.
If the length of the the bit string is less than the length of
isize, the result is sign-extended to 64 bits.#![allow(unused)] fn main() { use bit_byte_bit::{bits_as, Bits}; let bits = Bits::new([0x10, 0x32, 0x54, 0x76]); let signed = bits_as::signed(&bits); assert_eq!(signed, 0x0000000076543210); let bits = Bits::from([0x10, 0x32, 0x54, 0x76]); let signed = bits_as::signed(&bits); assert_eq!(signed, 0xFFFFFFFFF6543210u64 as isize); }
#![allow(unused)] fn main() { pub fn uint8(bits: &Bits) -> u8 }Converts up to the first 8 bits of a bit string to a 8 bit unsigned integer.
#![allow(unused)] fn main() { use bit_byte_bit::{bits_as, Bits}; let bits = Bits::new([0xFE]); let byte = bits_as::uint8(&bits); assert_eq!(byte, 0xFE); let bits = Bits::from([0x5E]); let byte = bits_as::uint8(&bits); assert_eq!(byte, 0x5E); }
#![allow(unused)] fn main() { pub fn uint16(bits: &Bits) -> u16 }Converts up the first 16 bits of a bit string to a 16 bit unsigned integer.
#![allow(unused)] fn main() { use bit_byte_bit::{bits_as, Bits}; let bits = Bits::new([0x10, 0xFE]); let ushort = bits_as::uint16(&bits); assert_eq!(ushort, 0xFE10); let bits = Bits::from([0x10, 0x5E]); let ushort = bits_as::uint16(&bits); assert_eq!(ushort, 0x5E10); }
#![allow(unused)] fn main() { pub fn uint32(bits: &Bits) -> u32 }Converts up to the first 32 bits of a bit string to a 32 bit unsigned integer.
#![allow(unused)] fn main() { use bit_byte_bit::{bits_as, Bits}; let bits = Bits::new([0x10, 0x32, 0x54, 0x76]); let uint = bits_as::uint32(&bits); assert_eq!(uint, 0x76543210); let bits = Bits::from([0x10, 0x32, 0x54, 0x76]); let uint = bits_as::uint32(&bits); assert_eq!(uint, 0x76543210); }
#![allow(unused)] fn main() { pub fn uint64(bits: &Bits) -> u64 }Converts up to the first 64 bits of a bit string to a 64 bit unsigned integer.
#![allow(unused)] fn main() { use bit_byte_bit::{bits_as, Bits}; let bits = Bits::new([0x10, 0x32, 0x54, 0x76]); let ulong = bits_as::uint64(&bits); assert_eq!(ulong, 0x0000000076543210); let bits = Bits::from([0x10, 0x32, 0x54, 0x76]); let ulong = bits_as::uint64(&bits); assert_eq!(ulong, 0x0000000076543210); }
#![allow(unused)] fn main() { pub fn unsigned(bits: &Bits) -> usize }Converts up to the first 64 bits of a bit string to an unsigned integer.
#![allow(unused)] fn main() { use bit_byte_bit::{bits_as, Bits}; let bits = Bits::new([0x10, 0x32, 0x54, 0x76]); let unsigned = bits_as::unsigned(&bits); assert_eq!(unsigned, 0x0000000076543210); let bits = Bits::from([0x10, 0x32, 0x54, 0x76]); let unsigned = bits_as::unsigned(&bits); assert_eq!(unsigned, 0x0000000076543210); }
#![allow(unused)] fn main() { pub fn vec_bool(bits: &Bits) -> Vec<bool> }Converts a bit string to a vector of booleans
#![allow(unused)] fn main() { use bit_byte_bit::{Bits, bits_as}; let bits = Bits::new([0x10, 0x32]); let bools = bits_as::vec_bool(&bits); assert_eq!(bools.len(), bits.len()); assert_eq!( bools, vec![ false, false, false, false, true, false, false, false, false, true, false, false, true, true, false, false ] ); }
#![allow(unused)] fn main() { pub fn vec_f32(bits: &Bits) -> Vec<f32> }Converts a bit string to a vector of 32 bit floats
#![allow(unused)] fn main() { use bit_byte_bit::{bits_as, Bits}; let bits = Bits::new([0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE]); let floats = bits_as::vec_f32(&bits); assert_eq!(floats.len(), 2); assert_eq!(floats, vec![f32::from_bits(0x76543210), f32::from_bits(0xFEDCBA98)]); let bits = Bits::new([0x10, 0x32, 0x54]); let floats = bits_as::vec_f32(&bits); assert_eq!(floats.len(), 1); assert_eq!(floats, vec![f32::from_bits(0x00543210)]); }
#![allow(unused)] fn main() { pub fn vec_f64(bits: &Bits) -> Vec<f64> }Converts a bit string to a vector of 64 bit floats
#![allow(unused)] fn main() { use bit_byte_bit::{bits_as, Bits}; let bits = Bits::new([0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE]); let floats = bits_as::vec_f64(&bits); assert_eq!(floats.len(), 1); assert_eq!(floats, vec![f64::from_bits(0xFEDCBA9876543210)]); let bits = Bits::new([0x10, 0x32, 0x54]); let floats = bits_as::vec_f64(&bits); assert_eq!(floats.len(), 1); assert_eq!(floats, vec![f64::from_bits(0x0000000000543210)]); }
#![allow(unused)] fn main() { pub fn vec_i8(bits: &Bits) -> Vec<i8> }Converts a bit string to a vector of 8 bit signed integers.
If the length of the the bit string is not divisible by 8 it is sign-extended to the next length that is, then it is converted.
#![allow(unused)] fn main() { use bit_byte_bit::{bits_as, Bits}; let bits = Bits::new([0x10, 0xFE]); let ibytes = bits_as::vec_i8(&bits); assert_eq!(ibytes.len(), 2); assert_eq!(ibytes, vec![0x10, 0xFEu8 as i8]); let bits = Bits::from([0x10, 0x5E]); let ibytes = bits_as::vec_i8(&bits); assert_eq!(ibytes.len(), 2); assert_eq!(ibytes, vec![0x10, 0xDEu8 as i8]); }
#![allow(unused)] fn main() { pub fn vec_i16(bits: &Bits) -> Vec<i16> }Converts a bit string to a vector of 16 bit signed integers.
If the length of the the bit string is not divisible by 16 it is sign-extended to the next length that is, then it is converted.
#![allow(unused)] fn main() { use bit_byte_bit::{bits_as, Bits}; let bits = Bits::new([0x10, 0xFE]); let ishorts = bits_as::vec_i16(&bits); assert_eq!(ishorts.len(), 1); assert_eq!(ishorts, vec![0xFE10u16 as i16]); let bits = Bits::from([0x10, 0x5E]); let ishorts = bits_as::vec_i16(&bits); assert_eq!(ishorts.len(), 1); assert_eq!(ishorts, vec![0xDE10u16 as i16]); }
#![allow(unused)] fn main() { pub fn vec_i32(bits: &Bits) -> Vec<i32> }Converts a bit string to a vector of 32 bit signed integers.
If the length of the the bit string is not divisible by 32 it is sign-extended to the next length that is, then it is converted.
#![allow(unused)] fn main() { use bit_byte_bit::{bits_as, Bits}; let bits = Bits::new([0x10, 0x32, 0x54, 0x76]); let ints = bits_as::vec_i32(&bits); assert_eq!(ints.len(), 1); assert_eq!(ints, vec![0x76543210]); let bits = Bits::from([0x10, 0x32, 0x54, 0x76]); let ints = bits_as::vec_i32(&bits); assert_eq!(ints.len(), 1); assert_eq!(ints, vec![0xF6543210u32 as i32]); }
#![allow(unused)] fn main() { pub fn vec_i64(bits: &Bits) -> Vec<i64> }Converts a bit string to a vector of 64 bit signed integers.
If the length of the the bit string is not divisible by 64 it is sign-extended to the next length that is, then it is converted.
#![allow(unused)] fn main() { use bit_byte_bit::{bits_as, Bits}; let bits = Bits::new([0x10, 0x32, 0x54, 0x76]); let longs = bits_as::vec_i64(&bits); assert_eq!(longs.len(), 1); assert_eq!(longs, vec![0x0000000076543210]); let bits = Bits::from([0x10, 0x32, 0x54, 0x76]); let longs = bits_as::vec_i64(&bits); assert_eq!(longs.len(), 1); assert_eq!(longs, vec![0xFFFFFFFFF6543210u64 as i64]); }
#![allow(unused)] fn main() { pub fn vec_isize(bits: &Bits) -> Vec<isize> }Converts a bit string to a vector of signed integers.
If the length of the the bit string is not divisible by the length of
isize, it is sign-extended to the next length that is, then it is converted.#![allow(unused)] fn main() { use bit_byte_bit::{bits_as, Bits}; let bits = Bits::new([0x10, 0x32, 0x54, 0x76]); let signed_ints = bits_as::vec_isize(&bits); assert_eq!(signed_ints.len(), 1); assert_eq!(signed_ints, vec![0x0000000076543210]); let bits = Bits::from([0x10, 0x32, 0x54, 0x76]); let signed_ints = bits_as::vec_i64(&bits); assert_eq!(signed_ints.len(), 1); assert_eq!(signed_ints, vec![0xFFFFFFFFF6543210u64 as i64]); }
#![allow(unused)] fn main() { pub fn vec_u8(bits: &Bits) -> Vec<u8> }Converts a bit string to a vector of 8 bit unsigned integers.
#![allow(unused)] fn main() { use bit_byte_bit::{bits_as, Bits}; let bits = Bits::new([0x10, 0xFE]); let bytes = bits_as::vec_u8(&bits); assert_eq!(bytes.len(), 2); assert_eq!(bytes, vec![0x10, 0xFE]); let bits = Bits::from([0x10, 0x5E]); let bytes = bits_as::vec_u8(&bits); assert_eq!(bytes.len(), 2); assert_eq!(bytes, vec![0x10, 0x5E]); }
#![allow(unused)] fn main() { pub fn vec_u16(bits: &Bits) -> Vec<u16> }Converts a bit string to a vector of 16 bit unsigned integers.
#![allow(unused)] fn main() { use bit_byte_bit::{bits_as, Bits}; let bits = Bits::new([0x10, 0xFE]); let shorts = bits_as::vec_u16(&bits); assert_eq!(shorts.len(), 1); assert_eq!(shorts, vec![0xFE10]); let bits = Bits::from([0x10, 0x5E]); let shorts = bits_as::vec_u16(&bits); assert_eq!(shorts.len(), 1); assert_eq!(shorts, vec![0x5E10]) }
#![allow(unused)] fn main() { pub fn vec_u32(bits: &Bits) -> Vec<u32> }Converts a bit string to a vector of 32 bit unsigned integers.
#![allow(unused)] fn main() { use bit_byte_bit::{bits_as, Bits}; let bits = Bits::new([0x10, 0x32, 0x54, 0x76]); let uints = bits_as::vec_u32(&bits); assert_eq!(uints.len(), 1); assert_eq!(uints, vec![0x76543210]); let bits = Bits::from([0x10, 0x32, 0x54, 0x76]); let uints = bits_as::vec_u32(&bits); assert_eq!(uints.len(), 1); assert_eq!(uints, vec![0x76543210]); }
#![allow(unused)] fn main() { pub fn vec_u64(bits: &Bits) -> Vec<u64> }Converts a bit string to a vector of 64 bit unsigned integers.
If the length of the the bit string is not divisible by 64 it is sign-extended to the next length that is, then it is converted.
#![allow(unused)] fn main() { use bit_byte_bit::{bits_as, Bits}; let bits = Bits::new([0x10, 0x32, 0x54, 0x76]); let ulongs = bits_as::vec_u64(&bits); assert_eq!(ulongs.len(), 1); assert_eq!(ulongs, vec![0x0000000076543210]); let bits = Bits::from([0x10, 0x32, 0x54, 0x76]); let ulongs = bits_as::vec_u64(&bits); assert_eq!(ulongs.len(), 1); assert_eq!(ulongs, vec![0x0000000076543210]); }
#![allow(unused)] fn main() { pub fn vec_usize(bits: &Bits) -> Vec<usize> }Converts a bit string to a vector of unsigned integers.
#![allow(unused)] fn main() { use bit_byte_bit::{bits_as, Bits}; let bits = Bits::new([0x10, 0x32, 0x54, 0x76]); let unsigned_ints = bits_as::vec_usize(&bits); assert_eq!(unsigned_ints.len(), 1); assert_eq!(unsigned_ints, vec![0x0000000076543210]); let bits = Bits::from([0x10, 0x32, 0x54, 0x76]); let unsigned_ints = bits_as::vec_usize(&bits); assert_eq!(unsigned_ints.len(), 1); assert_eq!(unsigned_ints, vec![0x0000000076543210]); }
Bits
#![allow(unused)] fn main() { pub struct Bits { /* private fields */ } }
Implements a byte-oriented bit string.
Implementation
#![allow(unused)] fn main() { pub fn new<I: IntoIterator<Item = u8>>(bits: I) -> Bits }Creates a bit string from a sequence of bytes.
#![allow(unused)] fn main() { let mut x = Bits::new([0x0A, 0x0B, 0x0C, 0x00]); assert_eq!(x.len(), 32); }
#![allow(unused)] fn main() { pub fn aligned<I: IntoIterator<Item = u8>>(units: usize, bits: I) -> Bits }Creates a bit string with an aligned length.
#![allow(unused)] fn main() { let mut x = Bits::aligned(17, [0x0A, 0x0B, 0x0C]); assert_eq!(x.len(), 34); assert_eq!(x, Bits::slice(&[0x0A, 0x0B, 0x0C, 0x00, 0x00], 34)); }
#![allow(unused)] fn main() { pub fn empty() -> Bits }Creates an empty bit string.
#![allow(unused)] fn main() { let mut x = Bits::empty(); assert_eq!(x.len(), 0); }
#![allow(unused)] fn main() { pub fn ones(length: usize) -> Bits }Creates a bit string of ones.
#![allow(unused)] fn main() { let mut x = Bits::ones(4); assert_eq!(x.len(), 4); assert_eq!(x, Bits::from([0x0F])); }
#![allow(unused)] fn main() { pub fn packed<I: IntoIterator<Item = u8>>(bytes: I) -> Bits }Creates a bit string by concatenating a slice of bytes removing leading zeros.
#![allow(unused)] fn main() { let x1 = Bits::packed([0x00, 0x00, 0x00]); assert_eq!(x1, Bits::empty()); let x2 = Bits::packed([0x0A, 0x0B, 0x0C]); assert_eq!(x2, Bits::from([0xBA, 0x0C])); assert_eq!(x2.len(), 12); }
#![allow(unused)] fn main() { pub fn slice(src: &[u8], length: usize) -> Bits }Creates a bit string by copying a given number of bits from a slice of bytes.
When the length is greater than the available number of bits in the source, the result is padded with zeros.
#![allow(unused)] fn main() { let x1 = Bits::slice(&[], 17); assert_eq!(x1, Bits::zeros(17)); let x2 = Bits::slice(&[0x0A, 0x0B, 0x0C], 0); assert_eq!(x2, Bits::empty()); let x3 = Bits::slice(&[0x0A, 0x0B, 0x0C], 19); assert_eq!(x3, Bits::from([0x0A, 0x0B, 0x04])); let x4 = Bits::slice(&[0x00, 0x00, 0x00], 19); assert_eq!(x4, Bits::zeros(19)); }
#![allow(unused)] fn main() { pub fn take(src: &[u8], start: usize, length: usize) -> Bits }Creates a bit string by copying a given range of bits from a slice of bytes.
When the length is greater than the available number of bits in the source, the result is padded with zeros.
#![allow(unused)] fn main() { let x1 = Bits::take(&[], 1, 17); assert_eq!(x1, Bits::zeros(17)); let x2 = Bits::take(&[0x0A, 0x0B, 0x0C], 1, 0); assert_eq!(x2, Bits::empty()); let x3 = Bits::take(&[0x0A, 0x0B, 0x0C], 25, 17); assert_eq!(x3, Bits::zeros(17)); let x4 = Bits::take(&[0x0A, 0x0B, 0x0C], 1, 17); assert_eq!(x4.bytes().to_vec(), vec![0x85, 0x05, 0x00]); let x5 = Bits::take(&[0x0A, 0x0B, 0x0C], 1, 16); assert_eq!(x5.bytes().to_vec(), vec![0x85, 0x05]); let x6 = Bits::take(&[0x00, 0x00, 0x00], 1, 17); assert_eq!(x6, Bits::zeros(17)); }
#![allow(unused)] fn main() { pub fn zeros(length: usize) -> Bits }Creates a bit string of zeros.
#![allow(unused)] fn main() { let mut x = Bits::zeros(4); assert_eq!(x.len(), 4); assert_eq!(x, Bits::from([false, false, false, false])); }#![allow(unused)] fn main() { pub fn align(&mut self, unit: usize) Pads a bit string until the length is a multiple of the given unit. ```rust let mut x = Bits::from([0x0A, 0x0B, 0x0C]); x.align(16); assert_eq!(x, Bits::new([0x0A, 0x0B, 0x0C, 0x00])); assert_eq!(x.len(), 32); }
#![allow(unused)] fn main() { pub fn all(&self) -> bool }Whether all bits are one.
#![allow(unused)] fn main() { let x1 = Bits::empty(); assert!(x1.all()); let x2 = Bits::from([0x0F]); assert!(x2.all()); let x3 = Bits::new([0x0F]); assert!(!x3.all()); }
#![allow(unused)] fn main() { pub fn and(&self, rhs: &Bits) -> Bits }Bitwise and of two bit strings.
#![allow(unused)] fn main() { let x1 = Bits::from([0x20, 0x30, 0x40]); let y1 = Bits::from([0xA0, 0xB0, 0xC0]); assert_eq!(x1.and(&y1), Bits::new([0x20, 0x30, 0x40])); let x2 = Bits::from([0x20, 0x30, 0x40]); let y2 = Bits::from([0x0A, 0xB0, 0xC0]); assert_eq!(x2.len(), 23); assert_eq!(y2.len(), 24); let z = x2.and(&y2); assert_eq!(z.len(), 24); }
#![allow(unused)] fn main() { pub fn and_mut(&mut self, rhs: &Bits) }Bitwise and of two bit strings.
#![allow(unused)] fn main() { let mut x1 = Bits::from([0x20, 0x30, 0x40]); let y1 = Bits::from([0xA0, 0xB0, 0xC0]); x1.and_mut(&y1); assert_eq!(x1, Bits::new([0x20, 0x30, 0x40])); let mut x2 = Bits::from([0x20, 0x30, 0x40]); let y2 = Bits::from([0x0A, 0xB0, 0xC0]); assert_eq!(x2.len(), 23); assert_eq!(y2.len(), 24); x2.and_mut(&y2); assert_eq!(x2.len(), 24); }
#![allow(unused)] fn main() { pub fn any(&self) -> bool }Whether at least one bit is one.
#![allow(unused)] fn main() { let x1 = Bits::empty(); assert!(!x1.any()); let x2 = Bits::from([0x0A, 0x0B, 0x0C]); assert!(x2.any()); let x3 = Bits::zeros(20); assert!(!x3.any()); }
#![allow(unused)] fn main() { pub fn byte(&self, i: usize) -> u8 }State of a byte.
#![allow(unused)] fn main() { let mut x = Bits::from([0x0A, 0x0B]); assert_eq!(x.byte(0), 0x0A); assert_eq!(x.byte(1), 0x0B); }ⓘ
#![allow(unused)] fn main() { let mut x = Bits::from([0x0A, 0x0B]); assert_eq!(x.byte(2), 0x0C); }
#![allow(unused)] fn main() { pub fn bytes(&self) -> &[u8] }Underlying bytes
#![allow(unused)] fn main() { let x = Bits::new([0xAB, 0xCD, 0xEF]); let y = x.bytes().iter().cloned().collect::<Vec<u8>>(); assert_eq!(y, vec![0xAB, 0xCD, 0xEF]); }
#![allow(unused)] fn main() { pub fn complement(&self) -> Bits }Flips every bit.
#![allow(unused)] fn main() { let x = Bits::from([0x0A, 0x0B, 0x0C]); assert_eq!(x.complement(), Bits::slice(&[0xF5, 0xF4, 0x03], 20)); }
#![allow(unused)] fn main() { pub fn complement_mut(&mut self) }Flips every bit.
#![allow(unused)] fn main() { let mut x1 = Bits::from([0x0A, 0x0B, 0x0C]); x1.complement_mut(); assert_eq!(x1, Bits::slice(&[0xF5, 0xF4, 0x03], 20)); }
#![allow(unused)] fn main() { pub fn consume_left(&mut self, count: usize) }Shifts out upper bits.
#![allow(unused)] fn main() { let mut x1 = Bits::from([0x0A, 0x0B, 0x0C]); x1.consume_left(4); assert_eq!(x1.len(), 16); assert_eq!(x1, Bits::new([0x0A, 0x0B])); let mut x2 = Bits::new([0x0A, 0x0B, 0x0C]); x2.consume_left(4); assert_eq!(x2.len(), 20); assert_eq!(x2, Bits::from([0x0A, 0x0B, 0x0C])); }
#![allow(unused)] fn main() { pub fn consume_right(&mut self, count: usize) }Shifts out lower bits.
#![allow(unused)] fn main() { let mut x1 = Bits::from([0x0A, 0x0B, 0x0C]); x1.consume_right(4); assert_eq!(x1, Bits::from([0xB0, 0xC0])); assert_eq!(x1.len(), 16); let mut x2 = Bits::new([0x0A, 0x0B, 0x0C]); x2.consume_right(4); assert_eq!(x2, Bits::slice(&[0xB0, 0xC0, 0x00], 20)); assert_eq!(x2.len(), 20); }
#![allow(unused)] fn main() { pub fn count_zeros(&self) -> usize }The number of zeros.
#![allow(unused)] fn main() { let mut x = Bits::from([15, 15, 15]); assert_eq!(x.count_zeros(), 8); x = Bits::new([15, 15, 15]); assert_eq!(x.count_zeros(), 12); }
#![allow(unused)] fn main() { pub fn extend_left(&mut self, bytes: &[u8]) }Adds upper bit padding to a bit string.
#![allow(unused)] fn main() { let mut x1 = Bits::from([0x0A, 0x0B, 0x0C]); x1.extend_left(&[0x0D, 0x0E, 0x0F]); assert_eq!(x1.len(), 44); assert_eq!(x1, Bits::slice(&[0x0A, 0x0B, 0xDC, 0xE0, 0xF0, 0x00], 44)); let mut x2 = Bits::new([0x0A, 0x0B, 0x0C]); x2.extend_left(&[0x0D, 0x0E, 0x0F]); assert_eq!(x2.len(), 48); assert_eq!(x2, Bits::new([0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F])); }
#![allow(unused)] fn main() { pub fn extend_right(&mut self, bytes: &[u8]) }Adds lower bit padding to a bit string.
#![allow(unused)] fn main() { let mut x1 = Bits::empty(); x1.extend_right(&[0x0A, 0x0B, 0x0C]); assert_eq!(x1.len(), 24); assert_eq!(x1, Bits::new([0x0A, 0x0B, 0x0C])); let mut x2 = Bits::from([0x0A, 0x0B, 0x0C]); x2.extend_right(&[0x0D, 0x0E, 0x0F]); assert_eq!(x2.len(), 44); assert_eq!(x2, Bits::from([0x0D, 0x0E, 0x0F, 0x0A, 0x0B, 0x0C])); }
#![allow(unused)] fn main() { pub fn hamming_weight(&self) -> usize }The number of ones.
#![allow(unused)] fn main() { let mut x = Bits::from([15, 15, 15]); assert_eq!(x.hamming_weight(), 12); }
#![allow(unused)] fn main() { pub fn i(&self, i: usize) -> u8 }State of a bit.
#![allow(unused)] fn main() { let mut x = Bits::from([0x0F]); assert_eq!(x.i(3), 1u8); }ⓘ
#![allow(unused)] fn main() { let mut x = Bits::from([0x0F]); assert_eq!(x.i(4), 0); }
#![allow(unused)] fn main() { pub fn into_bytes(self) -> IntoBytes }An iterator over the individual bits
#![allow(unused)] fn main() { let x = Bits::new([0xAB, 0xCD, 0xEF]); let y = x.into_bytes().collect::<Vec<u8>>(); assert_eq!(y, vec![0xAB, 0xCD, 0xEF]); }
#![allow(unused)] fn main() { pub fn iter(&self) -> Iter<'_> }An iterator over the individual bits
#![allow(unused)] fn main() { let x = Bits::new([0xAB, 0xCD, 0xEF]); let mut ones = 0; for bit in x.iter() { if bit == 1 { ones += 1; } } assert_eq!(ones, 17); }
#![allow(unused)] fn main() { pub fn iter_from(&self, index: usize) -> Iter<'_> }An iterator over the individual bits
#![allow(unused)] fn main() { let x = Bits::new([0xAB, 0xCD, 0xEF]); let mut ones = 0; for bit in x.iter_from(0) { if bit == 1 { ones += 1; } } assert_eq!(ones, 17); ones = 0; for bit in x.iter_from(13) { if bit == 1 { ones += 1; } } assert_eq!(ones, 9); }
#![allow(unused)] fn main() { pub fn leading_ones(&self) -> usize }The number of leading ones.
#![allow(unused)] fn main() { let x1 = Bits::from([0x0A, 0xFF, 0x03]); assert_eq!(x1.leading_ones(), 10); let x2 = Bits::new([0x0A, 0xFF, 0x0B]); assert_eq!(x2.leading_ones(), 0); }
#![allow(unused)] fn main() { pub fn leading_zeros(&self) -> usize }The number of leading zeros.
#![allow(unused)] fn main() { let x1 = Bits::from([0x0A, 0x00, 0x00]); assert_eq!(x1.leading_zeros(), 0); let x2 = Bits::zeros(20); assert_eq!(x2.leading_zeros(), 20); }
#![allow(unused)] fn main() { pub fn len(&self) -> usize }The number of bits.
#![allow(unused)] fn main() { let x1 = Bits::empty(); assert_eq!(x1.len(), 0); let x2 = Bits::from([0x0A, 0x0B, 0x0C]); assert_eq!(x2.len(), 20); let x3 = Bits::new([0x0A, 0x0B, 0x0C]); assert_eq!(x3.len(), 24); let x4 = Bits::slice(&[0x0A, 0x0B, 0x0C], 17); assert_eq!(x4.len(), 17); }
#![allow(unused)] fn main() { pub fn none(&self) -> bool }Whether all bits are zero.
#![allow(unused)] fn main() { let x1 = Bits::empty(); assert!(!x1.any()); let x2 = Bits::from([0x0A, 0x0B, 0x0C]); assert!(x2.any()); let x3 = Bits::zeros(20); assert!(!x3.any()); }
#![allow(unused)] fn main() { pub fn or(&self, rhs: &Bits) -> Bits }Bitwise or of two bit strings.
#![allow(unused)] fn main() { let x1 = Bits::from([0x20, 0x30, 0x40]); let y1 = Bits::from([0xA0, 0xB0, 0xC0]); assert_eq!(x1.or(&y1), Bits::from([0xA0, 0xB0, 0xC0])); let x2 = Bits::from([0x20, 0x30, 0x40]); let y2 = Bits::from([0x0A, 0xB0, 0xC0]); assert_eq!(x2.len(), 23); assert_eq!(y2.len(), 24); let z = x2.or(&y2); assert_eq!(z.len(), 24); }
#![allow(unused)] fn main() { pub fn or_mut(&mut self, rhs: &Bits) }Bitwise or of two bit strings.
#![allow(unused)] fn main() { let mut x1 = Bits::from([0x20, 0x30, 0x40]); let y1 = Bits::from([0xA0, 0xB0, 0xC0]); x1.or_mut(&y1); assert_eq!(x1, Bits::from([0xA0, 0xB0, 0xC0])); let mut x2 = Bits::from([0x20, 0x30, 0x40]); let y2 = Bits::from([0x0A, 0xB0, 0xC0]); assert_eq!(x2.len(), 23); assert_eq!(y2.len(), 24); x2.or_mut(&y2); assert_eq!(x2.len(), 24); }
#![allow(unused)] fn main() { pub fn pop_left(&mut self) -> u8 }Shifts out the most significant bit.
#![allow(unused)] fn main() { let mut x1 = Bits::empty(); assert_eq!(x1.pop_left(), 0u8); let mut x2 = Bits::from([0x0A, 0x0B, 0x0C]); assert_eq!(x2.pop_left(), 1u8); assert_eq!(x2.len(), 19); let mut x3 = Bits::new([0x0A, 0x0B, 0x0C]); assert_eq!(x3.pop_left(), 0u8); assert_eq!(x3.len(), 23); }
#![allow(unused)] fn main() { pub fn pop_right(&mut self) -> u8 }Shifts out the least significant bit.
#![allow(unused)] fn main() { let mut x1 = Bits::empty(); assert_eq!(x1.pop_right(), 0u8); let mut x2 = Bits::from([0x0A, 0x0B, 0x0C]); assert_eq!(x2.pop_right(), 0u8); assert_eq!(x2.len(), 19); assert_eq!(x2.pop_right(), 1u8); }
#![allow(unused)] fn main() { pub fn push_left(&mut self, bit: bool, count: usize) }Adds upper bit padding to a bit string.
#![allow(unused)] fn main() { let mut x1 = Bits::from([0x0A, 0x0B, 0x0C]); x1.push_left(true, 17); assert_eq!(x1.len(), 37); assert_eq!(x1, Bits::from([0x0A, 0x0B, 0xFC, 0xFF, 0x1F])); let mut x2 = Bits::new([0x0A, 0x0B, 0x0C]); x2.push_left(false, 17); assert_eq!(x2.len(), 41); assert_eq!(x2, Bits::slice(&[0x0A, 0x0B, 0x0C, 0x00, 0x00, 0x00], 41)); }
#![allow(unused)] fn main() { pub fn push_byte_left(&mut self, word: u8, count: usize) }Adds upper bit padding to a bit string.
#![allow(unused)] fn main() { let mut x = Bits::from([15, 15, 15]); x.push_byte_left(15, 2); assert_eq!(x.len(), 36); assert_eq!(x, Bits::slice(&[15, 15, 255, 240, 0], 36)); x = Bits::new([15, 15, 15]); x.push_byte_left(31, 2); assert_eq!(x.len(), 40); assert_eq!(x, Bits::new([15, 15, 15, 31, 31])); }
#![allow(unused)] fn main() { pub fn push_right(&mut self, bit: bool, count: usize) }Adds lower bit padding to a bit string.
#![allow(unused)] fn main() { let mut x = Bits::from([0x0A, 0x0B, 0x0C]); x.push_right(true, 4); assert_eq!(x.len(), 24); assert_eq!(x, Bits::from([0xAF, 0xB0, 0xC0])); }
#![allow(unused)] fn main() { pub fn push_byte_right(&mut self, word: u8, count: usize) }Adds lower bit padding to a bit string.
#![allow(unused)] fn main() { let mut x = Bits::from([15, 15, 15]); x.push_byte_right(31, 2); assert_eq!(x.len(), 36); assert_eq!(x, Bits::from([31, 31, 15, 15, 15])); }
#![allow(unused)] fn main() { pub fn reset(&mut self, i: usize) }Sets a bit to zero.
#![allow(unused)] fn main() { let mut x = Bits::new([15]); assert_eq!(x.i(3), 1u8); x.reset(3); assert_eq!(x.i(3), 0u8); }
#![allow(unused)] fn main() { pub fn reset_bits(&mut self, start: usize, count: usize) }Sets the state of a range of bits to zero.
#![allow(unused)] fn main() { let mut x1 = Bits::from([0x0A, 0x0B, 0x0C]); x1.reset_bits(7, 10); assert_eq!(x1, Bits::from([0x0A, 0x00, 0x0C])); let mut x2 = Bits::from([0x0A, 0x0B, 0x0C]); x2.reset_bits(8, 12); assert_eq!(x2, Bits::slice(&[0x0A, 0x00, 0x00], 20)); }ⓘ
#![allow(unused)] fn main() { let mut x = Bits::from([0x0A, 0x0B, 0x0C]); x.reset_bits(21, 14); }ⓘ
#![allow(unused)] fn main() { let mut x = Bits::from([0x0A, 0x0B, 0x0C]); x.reset_bits(7, 14); }
#![allow(unused)] fn main() { pub fn reset_byte(&mut self, i: usize) }Sets the state of a byte to zero.
#![allow(unused)] fn main() { let mut x = Bits::from([0x0A, 0x0B, 0x0C]); x.reset_byte(1); assert_eq!(x, Bits::from([0x0A, 0x00, 0x0C])); x.reset_byte(2); assert_eq!(x, Bits::slice(&[0x0A, 0x00, 0x00], 20)); }ⓘ
#![allow(unused)] fn main() { let mut x = Bits::from([0x0A, 0x0B, 0x0C]); x.reset_byte(3); }
#![allow(unused)] fn main() { pub fn reset_bytes(&mut self, start: usize, count: usize) }Sets the state of a range of bytes to zero.
#![allow(unused)] fn main() { let mut x = Bits::from([0x0A, 0x0B, 0x0C]); x.reset_bytes(1, 2); assert_eq!(x, Bits::slice(&[0x0A, 0x00, 0x00], 20)); }ⓘ
#![allow(unused)] fn main() { let mut x = Bits::from([0x0A, 0x0B, 0x0C]); x.reset_bytes(3, 2); }ⓘ
#![allow(unused)] fn main() { let mut x = Bits::from([0x0A, 0x0B, 0x0C]); x.reset_bytes(0, 4); }
#![allow(unused)] fn main() { pub fn reverse(&mut self) }Reverses the order of the bits.
#![allow(unused)] fn main() { let mut x1 = Bits::from([0x0A, 0x0B, 0x0C]); x1.reverse(); assert_eq!(x1, Bits::slice(&[0x03, 0x0D, 0x05], 20)); let mut x2 = Bits::new([0x0A, 0x0B, 0x0C]); x2.reverse(); assert_eq!(x2, Bits::new([0x30, 0xD0, 0x50])); }
#![allow(unused)] fn main() { pub fn rotate_left(&mut self, count: usize) }Rotates bits to the left.
#![allow(unused)] fn main() { let mut x1 = Bits::from([0x0A, 0x0B, 0x0C]); x1.rotate_left(4); assert_eq!(x1, Bits::slice(&[0xAC, 0xB0, 0x00], 20)); assert_eq!(x1.len(), 20); let mut x2 = Bits::new([0x0A, 0x0B, 0x0C]); x2.rotate_left(4); assert_eq!(x2, Bits::new([0xA0, 0xB0, 0xC0])); assert_eq!(x2.len(), 24); }
#![allow(unused)] fn main() { pub fn rotated_left(&self, count: usize) -> Bits }Rotates bits to the left.
#![allow(unused)] fn main() { let mut x1 = Bits::from([0x0A, 0x0B, 0x0C]); assert_eq!(x1.rotated_left(4), Bits::slice(&[0xAC, 0xB0, 0x00], 20)); assert_eq!(x1.len(), 20); let mut x2 = Bits::new([0x0A, 0x0B, 0x0C]); assert_eq!(x2.rotated_left(4), Bits::new([0xA0, 0xB0, 0xC0])); assert_eq!(x2.len(), 24); }
#![allow(unused)] fn main() { pub fn rotate_right(&mut self, count: usize) }Rotates bits to the right.
#![allow(unused)] fn main() { let mut x1 = Bits::from([0x0A, 0x0B, 0x0C]); x1.rotate_right(4); assert_eq!(x1, Bits::slice(&[0xB0, 0xC0, 0x0A], 20)); assert_eq!(x1.len(), 20); let mut x2 = Bits::new([0x0A, 0x0B, 0x0C]); x2.rotate_right(4); assert_eq!(x2, Bits::new([0xB0, 0xC0, 0xA0])); assert_eq!(x2.len(), 24); }
#![allow(unused)] fn main() { pub fn rotated_right(&self, count: usize) -> Bits }Rotates bits to the right.
#![allow(unused)] fn main() { let mut x1 = Bits::from([0x0A, 0x0B, 0x0C]); assert_eq!(x1.rotated_right(4), Bits::slice(&[0xB0, 0xC0, 0x0A], 20)); assert_eq!(x1.len(), 20); let mut x2 = Bits::new([0x0A, 0x0B, 0x0C]); assert_eq!(x2.rotated_right(4), Bits::new([0xB0, 0xC0, 0xA0])); assert_eq!(x2.len(), 24); }
#![allow(unused)] fn main() { pub fn set(&mut self, i: usize) }Sets a bit to one.
#![allow(unused)] fn main() { let mut x = Bits::new([15]); assert_eq!(x.i(4), 0u8); x.set(4); assert_eq!(x.i(4), 1u8); }
#![allow(unused)] fn main() { pub fn set_bits(&mut self, start: usize, count: usize) }Sets the state of a range of bits to one.
#![allow(unused)] fn main() { let mut x1 = Bits::from([0x0A, 0x0B, 0x0C]); x1.set_bits(7, 10); assert_eq!(x1, Bits::from([0x8A, 0xFF, 0x0D])); let mut x2 = Bits::from([0x0A, 0x0B, 0x0C]); x2.set_bits(8, 12); assert_eq!(x2, Bits::slice(&[0x0A, 0xFF, 0x0F], 20)); }ⓘ
#![allow(unused)] fn main() { let mut x = Bits::from([0x0A, 0x0B, 0x0C]); x.set_bits(21, 14); }ⓘ
#![allow(unused)] fn main() { let mut x = Bits::from([0x0A, 0x0B, 0x0C]); x.set_bits(7, 14); }
#![allow(unused)] fn main() { pub fn set_byte(&mut self, i: usize) }Sets the state of a byte to all ones.
#![allow(unused)] fn main() { let mut x = Bits::from([0x0A, 0x0B, 0x0C]); x.set_byte(1); assert_eq!(x, Bits::from([0x0A, 0xFF, 0x0C])); x.set_byte(2); assert_eq!(x, Bits::slice(&[0x0A, 0xFF, 0x0F], 20)); }ⓘ
#![allow(unused)] fn main() { let mut x = Bits::from([0x0A, 0x0B, 0x0C]); x.toggle_byte(3); }
#![allow(unused)] fn main() { pub fn set_bytes(&mut self, start: usize, count: usize) }Sets the state of a range of bytes to all ones.
#![allow(unused)] fn main() { let mut x = Bits::from([0x0A, 0x0B, 0x0C]); x.set_bytes(1, 2); assert_eq!(x, Bits::slice(&[0x0A, 0xFF, 0x0F], 20)); }ⓘ
#![allow(unused)] fn main() { let mut x = Bits::from([0x0A, 0x0B, 0x0C]); x.set_bytes(3, 2); }ⓘ
#![allow(unused)] fn main() { let mut x = Bits::from([0x0A, 0x0B, 0x0C]); x.set_bytes(0, 4); }
#![allow(unused)] fn main() { pub fn shift_left(&mut self, count: usize) }Shifts out upper bits, shifting in zeros on the lower end.
#![allow(unused)] fn main() { let mut x1 = Bits::from([0x0A, 0x0B, 0x0C]); x1.shift_left(17); assert_eq!(x1, Bits::slice(&[0x00, 0x00, 0x04], 20)); assert_eq!(x1.len(), 20); let mut x2 = Bits::new([0x0A, 0x0B, 0x0C]); x2.shift_left(4); assert_eq!(x2, Bits::new([0xA0, 0xB0, 0xC0])); assert_eq!(x2.len(), 24); }
#![allow(unused)] fn main() { pub fn shifted_left(&self, count: usize) -> Bits }Shifts out upper bits, shifting in zeros on the lower end.
#![allow(unused)] fn main() { let x1 = Bits::from([0x0A, 0x0B, 0x0C]); let s1 = x1.shifted_left(17); assert_eq!(s1, Bits::slice(&[0x00, 0x00, 0x04], 20)); assert_eq!(s1.len(), 20); let x2 = Bits::new([0x0A, 0x0B, 0x0C]); let s2 = x2.shifted_left(4); assert_eq!(s2, Bits::new([0xA0, 0xB0, 0xC0])); assert_eq!(s2.len(), 24); }
#![allow(unused)] fn main() { pub fn shift_right(&mut self, count: usize) }Shifts out lower bits, shifting zeros into the upper bits.
#![allow(unused)] fn main() { let mut x1 = Bits::from([0x0A, 0x0B, 0x0C]); x1.shift_right(17); assert_eq!(x1.len(), 20); assert_eq!(x1, Bits::slice(&[0x06, 0x00, 0x00], 20)); let mut x2 = Bits::new([0x0A, 0x0B, 0x0C]); x2.shift_right(4); assert_eq!(x2.len(), 24); assert_eq!(x2, Bits::new([0xB0, 0xC0, 0x00])); }
#![allow(unused)] fn main() { pub fn shifted_right(&self, count: usize) -> Bits }Shifts out lower bits, shifting zeros into the upper bits.
#![allow(unused)] fn main() { let x1 = Bits::from([0x0A, 0x0B, 0x0C]); let s1 = x1.shifted_right(17); assert_eq!(s1.len(), 20); assert_eq!(s1, Bits::slice(&[0x06, 0x00, 0x00], 20)); let x2 = Bits::new([0x0A, 0x0B, 0x0C]); let s2 = x2.shifted_right(4); assert_eq!(s2.len(), 24); assert_eq!(s2, Bits::new([0xB0, 0xC0, 0x00])); }
#![allow(unused)] fn main() { pub fn size(&self) -> usize }The number of bytes.
#![allow(unused)] fn main() { let x1 = Bits::empty(); assert_eq!(x1.size(), 0); let x2 = Bits::from([0x0A, 0x0B, 0x0C]); assert_eq!(x2.size(), 3); let x3 = Bits::new([0x0A, 0x0B, 0x0C]); assert_eq!(x3.size(), 3); let x4 = Bits::slice(&[0x0A, 0x0B, 0x0C], 13); assert_eq!(x4.size(), 2); }
#![allow(unused)] fn main() { pub fn split(&self, i: usize) -> (Bits, Bits) }Splits the bit string.
When the index is greater than or equal to the length of the bit string, the second element of the returned pair will be empty. When the index is 0, the first element will be empty.
#![allow(unused)] fn main() { let x1 = Bits::empty(); let (l1, r1) = x1.split(11); assert_eq!(l1.len(), 0); assert_eq!(r1.len(), 0); let x2 = Bits::from([0x0A, 0x0B, 0x0C]); let (l2, r2) = x2.split(20); assert_eq!(l2, x2); assert_eq!(r2.len(), 0); let (l3, r3) = x2.split(0); assert_eq!(l3.len(), 0); assert_eq!(r3, x2); let (l4, r4) = x2.split(11); assert_eq!(l4, Bits::slice(&[0x0A, 0x03], 11)); assert_eq!(r4, Bits::slice(&[0x81, 0x01], 9)); }
#![allow(unused)] fn main() { pub fn test(&self, i: usize) -> bool }Whether a bit is one.
#![allow(unused)] fn main() { let mut x = Bits::from([0x0F]); assert!(x.test(3)); assert!(!x.test(4)); assert!(!x.test(8)); }
#![allow(unused)] fn main() { pub fn toggle(&mut self, i: usize) }Flips the state of a bit.
#![allow(unused)] fn main() { let mut x = Bits::from([0x0A, 0x0B, 0x0C]); assert_eq!(x.i(0), 0u8); assert_eq!(x.i(1), 1u8); x.toggle(0); x.toggle(1); assert_eq!(x.i(0), 1u8); assert_eq!(x.i(1), 0u8); }ⓘ
#![allow(unused)] fn main() { let mut x = Bits::from([0x0A, 0x0B, 0x0C]); x.toggle(21); }
#![allow(unused)] fn main() { pub fn toggle_bits(&mut self, start: usize, count: usize) }Flips the state of a range of bits.
#![allow(unused)] fn main() { let mut x1 = Bits::from([0x0A, 0x0B, 0x0C]); x1.toggle_bits(7, 10); assert_eq!(x1, Bits::from([0x8A, 0xF4, 0x0D])); let mut x2 = Bits::from([0x0A, 0x0B, 0x0C]); x2.toggle_bits(8, 12); assert_eq!(x2, Bits::slice(&[0x0A, 0xF4, 0x03], 20)); }ⓘ
#![allow(unused)] fn main() { let mut x = Bits::from([0x0A, 0x0B, 0x0C]); x.toggle_bits(21, 14); }ⓘ
#![allow(unused)] fn main() { let mut x = Bits::from([0x0A, 0x0B, 0x0C]); x.toggle_bits(7, 14); }
#![allow(unused)] fn main() { pub fn toggle_byte(&mut self, i: usize) }Flips the state of a byte.
#![allow(unused)] fn main() { let mut x = Bits::from([0x0A, 0x0B, 0x0C]); x.toggle_byte(1); assert_eq!(x, Bits::from([0x0A, 0xF4, 0x0C])); x.toggle_byte(2); assert_eq!(x, Bits::slice(&[0x0A, 0xF4, 0x03], 20)); }ⓘ
#![allow(unused)] fn main() { let mut x = Bits::from([0x0A, 0x0B, 0x0C]); x.toggle_byte(3); }
#![allow(unused)] fn main() { pub fn toggle_bytes(&mut self, start: usize, count: usize) }Flips the state of a range of bytes.
#![allow(unused)] fn main() { let mut x = Bits::from([0x0A, 0x0B, 0x0C]); x.toggle_bytes(1, 2); assert_eq!(x, Bits::slice(&[0x0A, 0xF4, 0x03], 20)); }ⓘ
#![allow(unused)] fn main() { let mut x = Bits::from([0x0A, 0x0B, 0x0C]); x.toggle_bytes(3, 2); }ⓘ
#![allow(unused)] fn main() { let mut x = Bits::from([0x0A, 0x0B, 0x0C]); x.toggle_bytes(0, 4); }
#![allow(unused)] fn main() { pub fn trailing_ones(&self) -> usize }The number of trailing ones.
#![allow(unused)] fn main() { let x = Bits::from([0xFF, 0x0B, 0x0A]); assert_eq!(x.trailing_ones(), 10); }
#![allow(unused)] fn main() { pub fn trailing_zeros(&self) -> usize }The number of trailing zeros.
#![allow(unused)] fn main() { let x1 = Bits::from([0x00, 0x0A, 0x0B]); assert_eq!(x1.trailing_zeros(), 9); let x2 = Bits::zeros(20); assert_eq!(x2.trailing_zeros(), 20); }
#![allow(unused)] fn main() { pub fn trim_end(&mut self, bit: bool) }Trims leading bits.
#![allow(unused)] fn main() { let mut x1 = Bits::from([0x0A, 0x0B, 0x0C]); x1.trim_end(false); assert_eq!(x1.len(), 20); assert_eq!(x1, Bits::from([0x0A, 0x0B, 0x0C])); x1.trim_end(true); assert_eq!(x1.len(), 18); assert_eq!(x1, Bits::slice(&[0x0A, 0x0B, 0x00], 18)); let mut x2 = Bits::new([0x0A, 0x0B, 0x0C]); x2.trim_end(true); assert_eq!(x2.len(), 24); assert_eq!(x2, Bits::new([0x0A, 0x0B, 0x0C])); x2.trim_end(false); assert_eq!(x2.len(), 20); assert_eq!(x2, Bits::from([0x0A, 0x0B, 0x0C])); let mut x3 = Bits::slice(&[0x0A, 0x0B, 0x00], 18); x3.trim_end(false); assert_eq!(x3.len(), 12); assert_eq!(x3, Bits::from([0x0A, 0x0B])); let mut x4 = Bits::slice(&[0x0A, 0xFB, 0x03], 18); x4.trim_end(true); assert_eq!(x4.len(), 11); assert_eq!(x4, Bits::slice(&[0x0A, 0x0B], 11)); }
#![allow(unused)] fn main() { pub fn trim_start(&mut self, bit: bool) }Trims trailing bits.
#![allow(unused)] fn main() { let mut x1 = Bits::from([0x0A, 0x0B, 0x0C]); x1.trim_start(true); assert_eq!(x1.len(), 20); assert_eq!(x1, Bits::from([0x0A, 0x0B, 0x0C])); x1.trim_start(false); assert_eq!(x1.len(), 19); assert_eq!(x1, Bits::from([0x85, 0x05, 0x06])); let mut x2 = Bits::new([0x00, 0x0A, 0x0B]); x2.trim_start(false); assert_eq!(x2.len(), 15); assert_eq!(x2, Bits::slice(&[0x85, 0x05], 15)); let mut x3 = Bits::new([0xFF, 0x0B, 0x0C]); x3.trim_start(true); assert_eq!(x3.len(), 14); assert_eq!(x3, Bits::slice(&[0x02, 0x03], 14)); }
#![allow(unused)] fn main() { pub fn xor(&self, rhs: &Bits) -> Bits }Bitwise exclusive or of two bit strings.
#![allow(unused)] fn main() { let x1 = Bits::from([0x20, 0x30, 0x40]); let y1 = Bits::from([0xA0, 0xB0, 0xC0]); assert_eq!(x1.xor(&y1), Bits::from([0x80, 0x80, 0x80])); let x2 = Bits::from([0x20, 0x30, 0x40]); let y2 = Bits::from([0x0A, 0xB0, 0xC0]); assert_eq!(x2.len(), 23); assert_eq!(y2.len(), 24); let z = x2.xor(&y2); assert_eq!(z.len(), 24); }
#![allow(unused)] fn main() { pub fn xor_mut(&mut self, rhs: &Bits) }Bitwise exclusive or of two bit strings.
#![allow(unused)] fn main() { let mut x1 = Bits::from([0x20, 0x30, 0x40]); let y1 = Bits::from([0xA0, 0xB0, 0xC0]); x1.xor_mut(&y1); assert_eq!(x1, Bits::from([0x80, 0x80, 0x80])); let mut x2 = Bits::from([0x20, 0x30, 0x40]); let y2 = Bits::from([0x0A, 0xB0, 0xC0]); assert_eq!(x2.len(), 23); assert_eq!(y2.len(), 24); x2.xor_mut(&y2); assert_eq!(x2.len(), 24); }
#![allow(unused)] fn main() { pub fn xrange<R>(bounds: R) -> Bits where R: RangeBounds<usize> }Creates a bit string by copying a range of bits.
The range is truncated to take no more than the number of available bits from the starting bound. For alternative behavior, consider [Bits::take], which pads zeros when the length exceeds the number of available bits.
#![allow(unused)] fn main() { let x1 = Bits::empty(); let x1_r1 = x1.xrange(1..18); assert_eq!(x1_r1, Bits::empty()); let x2 = Bits::zeros(24); let x2_r1 = x2.xrange(8..24); let x2_r2 = x2.xrange(8..=23); assert_eq!(x2_r1, Bits::zeros(16)); assert_eq!(x2_r2, x2_r1); let x3 = Bits::from([0x0A, 0x0B, 0x0C]); let x3_r1 = x3.xrange(1..2); assert_eq!(x3_r1, Bits::from([0x01])); let x3_r2 = x3.xrange(0..); let x3_r3 = x3.xrange(..); assert_eq!(x3_r2, x3); assert_eq!(x3_r3, x3_r2); let x3_r4 = x3.xrange(..32); assert_eq!(x3_r4, Bits::from([0x0A, 0x0B, 0x0C])); assert_eq!(x3_r4.len(), 20); let x3_r5 = x3.xrange(18..32); assert_eq!(x3_r5, Bits::from([0x3])); assert_eq!(x3_r5.len(), 2); let x3_r6 = x3.xrange(8..0); assert_eq!(x3_r6, Bits::empty()); }
Traits
Binary
#![allow(unused)] fn main() { fn fmt(&self, f: &mut Formatter<'_>) -> Result }Formats the value using the given formatter.
BitAnd
#![allow(unused)] fn main() { fn bitand(self, rhs: Bits) -> Bits fn bitand(self, rhs: &Bits) -> Bits fn bitand(&'a self, rhs: Bits) -> Bits fn bitand(&self, rhs: &Bits) -> Bits }Bitwise and of two bit strings.
#![allow(unused)] fn main() { type Output = Bits }The resulting type after applying the & operator.
#![allow(unused)] fn main() { let x1 = Bits::from([0x20, 0x30, 0x40]); let y1 = Bits::from([0xA0, 0xB0, 0xC0]); assert_eq!(x1 & &y1, Bits::new([0x20, 0x30, 0x40])); let x2 = Bits::from([0x20, 0x30, 0x40]); let y2 = Bits::from([0x0A, 0xB0, 0xC0]); assert_eq!(x2.len(), 23); assert_eq!(y2.len(), 24); let z = x2 & &y2; assert_eq!(z.len(), 24); }
BitAndAssign
#![allow(unused)] fn main() { fn bitand_assign(&mut self, rhs: Bits) fn bitand_assign(&mut self, rhs: &Bits) }Bitwise and of two bit strings.
#![allow(unused)] fn main() { let mut x1 = Bits::from([0x20, 0x30, 0x40]); let y1 = Bits::from([0xA0, 0xB0, 0xC0]); x1 &= &y1; assert_eq!(x1, Bits::new([0x20, 0x30, 0x40])); let mut x2 = Bits::from([0x20, 0x30, 0x40]); let y2 = Bits::from([0x0A, 0xB0, 0xC0]); assert_eq!(x2.len(), 23); assert_eq!(y2.len(), 24); x2 &= &y2; assert_eq!(x2.len(), 24); }
BitOr
#![allow(unused)] fn main() { fn bitor(self, rhs: Bits) -> Bits fn bitor(self, rhs: &Bits) -> Bits fn bitor(&'a self, rhs: Bits) -> Bits fn bitor(&self, rhs: &Bits) -> Bits }Bitwise or of two bit strings.
#![allow(unused)] fn main() { type Output = Bits }The resulting type after applying the | operator.
#![allow(unused)] fn main() { let x1 = Bits::from([0x20, 0x30, 0x40]); let y1 = Bits::from([0xA0, 0xB0, 0xC0]); assert_eq!(x1 | &y1, Bits::from([0xA0, 0xB0, 0xC0])); let x2 = Bits::from([0x20, 0x30, 0x40]); let y2 = Bits::from([0x0A, 0xB0, 0xC0]); assert_eq!(x2.len(), 23); assert_eq!(y2.len(), 24); let z = x2 | &y2; assert_eq!(z.len(), 24); }
BitOrAssign
#![allow(unused)] fn main() { fn bitor_assign(&mut self, rhs: Bits) fn bitor_assign(&mut self, rhs: &Bits) }Bitwise or of two bit strings.
#![allow(unused)] fn main() { let mut x1 = Bits::from([0x20, 0x30, 0x40]); let y1 = Bits::from([0xA0, 0xB0, 0xC0]); x1 |= &y1; assert_eq!(x1, Bits::from([0xA0, 0xB0, 0xC0])); let mut x2 = Bits::from([0x20, 0x30, 0x40]); let y2 = Bits::from([0x0A, 0xB0, 0xC0]); assert_eq!(x2.len(), 23); assert_eq!(y2.len(), 24); x2 |= &y2; assert_eq!(x2.len(), 24); }
BitXor
#![allow(unused)] fn main() { fn bitxor(self, rhs: Bits) -> Bits fn bitxor(self, rhs: &Bits) -> Bits fn bitxor(&'a self, rhs: Bits) -> Bits fn bitxor(&self, rhs: &Bits) -> Bits }Bitwise exclusive or of two bit strings.
#![allow(unused)] fn main() { type Output = Bits }The resulting type after applying the ^ operator.
#![allow(unused)] fn main() { let x1 = Bits::from([0x20, 0x30, 0x40]); let y1 = Bits::from([0xA0, 0xB0, 0xC0]); assert_eq!(x1 ^ &y1, Bits::from([0x80, 0x80, 0x80])); let x2 = Bits::from([0x20, 0x30, 0x40]); let y2 = Bits::from([0x0A, 0xB0, 0xC0]); assert_eq!(x2.len(), 23); assert_eq!(y2.len(), 24); let z = x2 ^ &y2; assert_eq!(z.len(), 24); }
BitXorAssign
#![allow(unused)] fn main() { fn bitxor_assign(&mut self, rhs: Bits) fn bitxor_assign(&mut self, rhs: &Bits) }Bitwise exclusive or of two bit strings.
#![allow(unused)] fn main() { let mut x1 = Bits::from([0x20, 0x30, 0x40]); let y1 = Bits::from([0xA0, 0xB0, 0xC0]); x1 ^= &y1; assert_eq!(x1, Bits::from([0x80, 0x80, 0x80])); let mut x2 = Bits::from([0x20, 0x30, 0x40]); let y2 = Bits::from([0x0A, 0xB0, 0xC0]); assert_eq!(x2.len(), 23); assert_eq!(y2.len(), 24); x2 ^= &y2; assert_eq!(x2.len(), 24); }
Clone
#![allow(unused)] fn main() { fn clone(&self) -> Bits }Returns a copy of the value.
#![allow(unused)] fn main() { fn clone_from(&mut self, source: &Bits) }Performs copy-assignment from source.
Debug
#![allow(unused)] fn main() { fn fmt(&self, f: &mut Formatter<'_>) -> Result }Formats the value using the given formatter.
Display
#![allow(unused)] fn main() { fn fmt(&self, f: &mut Formatter<'_>) -> Result }Formats the value using the given formatter.
Div
#![allow(unused)] fn main() { fn div(&self, index: usize) -> (Bits, Bits) fn div(self, index: usize) -> (Bits, Bits) }Splits the bit string.
When the index is greater than or equal to the length of the bit string, the second element of the returned pair will be empty. When the index is
0, the first element will be empty.#![allow(unused)] fn main() { type Output = (Bits, Bits) }The resulting type after applying splitting the bit string.
#![allow(unused)] fn main() { let x1 = Bits::empty(); let (l1, r1) = &x1 / 11; assert_eq!(l1.len(), 0); assert_eq!(r1.len(), 0); let x2 = Bits::from([0x0A, 0x0B, 0x0C]); let (l2, r2) = &x2 / 20; assert_eq!(l2, x2); assert_eq!(r2.len(), 0); let (l3, r3) = &x2 / 0; assert_eq!(l3.len(), 0); assert_eq!(r3, x2); let (l4, r4) = x2 / 11; assert_eq!(l4, Bits::slice(&[0x0A, 0x03], 11)); assert_eq!(r4, Bits::slice(&[0x81, 0x01], 9)); }
Drop
#![allow(unused)] fn main() { fn drop(&mut self) }Executes the destructor for this type.
From
#![allow(unused)] fn main() { fn from(bits: &[bool]) -> Bits fn from(bits: [bool; N]) -> Bits fn from(bits: Vec<bool>) -> Bits fn from(data: &[u8]) -> Bits fn from(bytes: [u8; N]) -> Bits fn from(bytes: Vec<u8>) -> Bits }Creates a bit string from the given sequence. When the input is a sequence of
u8, the resulting bit string is trimmed of leading zeros.#![allow(unused)] fn main() { let bools = vec![true, false, true, true, false, true, true]; let bits = Bits::from(bools.as_slice()); assert_eq!(bits.len(), 7); assert_eq!(bits, Bits::from([0x6D])); let bools = vec![true, false, true, false, false, false, false, false, false]; let bits = Bits::from(bools); assert_eq!(bits.len(), 9); assert_eq!(bits, Bits::slice(&[0x05, 0x00], 9)); let bits = Bits::from([0x0A, 0x0B, 0x0C, 0x00]); assert_eq!(x.len(), 20); }
From<Bits>
#![allow(unused)] fn main() { fn from(bits: Bits) -> f32 fn from(bits: Bits) -> f64 fn from(bits: Bits) -> i8 fn from(bits: Bits) -> i16 fn from(bits: Bits) -> i32 fn from(bits: Bits) -> i64 fn from(bits: Bits) -> isize fn from(bits: Bits) -> u8 fn from(bits: Bits) -> u32 fn from(bits: Bits) -> u64 fn from(bits: Bits) -> usize fn from(bits: Bits) -> Vec<bool> fn from(bits: Bits) -> Vec<f32> fn from(bits: Bits) -> Vec<f64> fn from(bits: Bits) -> Vec<i8> fn from(bits: Bits) -> Vec<i16> fn from(bits: Bits) -> Vec<i32> fn from(bits: Bits) -> Vec<i64> fn from(bits: Bits) -> Vec<isize> fn from(bits: Bits) -> Vec<u8> fn from(bits: Bits) -> Vec<u16> fn from(bits: Bits) -> Vec<u32> fn from(bits: Bits) -> Vec<u64> fn from(bits: Bits) -> Vec<usize> }Converts a bit string to a given type. For a non-consuming version of each implementation use the
bits_asmodule.#![allow(unused)] fn main() { let bits = Bits::new([0x10, 0x32]); let bools = Vec::<bool>::from(&bits); assert_eq!(bools.len(), bits.len()); assert_eq!( bools, vec![ false, false, false, false, true, false, false, false, false, true, false, false, true, true, false, false ] ); let bits = Bits::new([0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE]); let floats = Vec::<f32>::from(&bits); assert_eq!(floats.len(), 2); assert_eq!(floats, vec![f32::from_bits(0x76543210), f32::from_bits(0xFEDCBA98)]); let floats = Vec::<f32>::from(&bits.xrange(0..24)); assert_eq!(floats.len(), 1); assert_eq!(floats, vec![f32::from_bits(0x00543210)]); let bits = Bits::new([0x10, 0xFE]); let ishorts = Vec::<i16>::from(&bits); assert_eq!(ishorts.len(), 1); assert_eq!(ishorts, vec![0xFE10u16 as i16]); let bits = Bits::from([0x10, 0x5E]); let ishorts = Vec::<i16>::from(&bits); assert_eq!(ishorts.len(), 1); assert_eq!(ishorts, vec![0xDE10u16 as i16]); let bits = Bits::new([0x10, 0x32, 0x54, 0x76]); let uints = Vec::<u32>::from(&bits); assert_eq!(uints.len(), 1); assert_eq!(uints, vec![0x76543210]); let bits = Bits::from([0x10, 0x32, 0x54, 0x76]); let uints = Vec::<u32>::from(&bits); assert_eq!(uints.len(), 1); assert_eq!(uints, vec![0x76543210]); let bits = Bits::new([0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE]); let float = f64::from(&bits); assert_eq!(float, f64::from_bits(0xFEDCBA9876543210)); let bits = Bits::new([0x10, 0x32, 0x54]); let float = f64::from(&bits); assert_eq!(float, f64::from_bits(0x0000000000543210)); let bits = Bits::new([0xFE]); let ibyte = i8::from(&bits); assert_eq!(ibyte, 0xFEu8 as i8); let bits = Bits::from([0x5E]); let ibyte = i8::from(&bits); assert_eq!(ibyte, 0xDEu8 as i8); let byte = u8::from(&bits); assert_eq!(byte, 0x5E); }
FromIterator
#![allow(unused)] fn main() { fn from_iter<I: IntoIterator<Item = bool>>(iter: I) -> Bits fn from_iter<I: IntoIterator<Item = u8>>(iter: I) -> Bits }Creates a value from an iterator.
IntoIterator
#![allow(unused)] fn main() { fn into_iter(self) -> Bits::IntoIter }Creates an iterator from a value.
#![allow(unused)] fn main() { type Item = u8 }The type of the elements being iterated over.
#![allow(unused)] fn main() { type IntoIter = IntoBits }Which kind of iterator are we turning this into?
Not
#![allow(unused)] fn main() { fn not(self) -> Bits fn not(&self) -> Bits }Flips every bit.
#![allow(unused)] fn main() { type Output = Bits }The resulting type after applying the ! operator.
#![allow(unused)] fn main() { let x = Bits::from([0x0A, 0x0B, 0x0C]); assert_eq!(!&x, Bits::slice(&[0xF5, 0xF4, 0x03], 20)); }
Ord
#![allow(unused)] fn main() { fn cmp(&self, other: &Bits) -> Ordering }Compares a bit string with another
#![allow(unused)] fn main() { let x = Bits::from([0x0A, 0x0B, 0x0C]); let y = Bits::from([0x09, 0x0B, 0x0C]); let z = Bits::from([0x0A, 0x09, 0x0D]); assert_eq!(x.cmp(&y), Ordering::Greater); assert_eq!(x.cmp(&z), Ordering::Less); }
#![allow(unused)] fn main() { fn max(self, other: Bits) -> Bits where Bits: Sized }Compares and returns the maximum of two values.
#![allow(unused)] fn main() { fn min(self, other: Bits) -> Bits where Bits: Sized }Compares and returns the minimum of two values.
#![allow(unused)] fn main() { fn clamp(self, min: Bits, max: Bits) -> Bits where Bits: Sized }Restrict a value to a certain interval.
PartialEq
#![allow(unused)] fn main() { fn eq(&self, other: &Bits) -> bool }Whether two bit strings are equal.
#![allow(unused)] fn main() { let x = Bits::from([0x20, 0x30, 0x40]); assert!(Bits::eq(&x, &x)); let y = Bits::from([0xA0, 0xB0, 0xC0]); assert!(Bits::ne(&x, &y)); let z1 = Bits::from([0x20, 0x30, 0x40, 0x00]); assert!(Bits::eq(&x, &z1)); let z2 = Bits::new([0x20, 0x30, 0x40, 0x00]); assert!(Bits::ne(&x, &z2)); }
#![allow(unused)] fn main() { fn ne(&self, other: &Rhs) -> bool }Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
PartialOrd
#![allow(unused)] fn main() { fn partial_cmp(&self, other: &Bits) -> Option<Ordering> }Compares a bit string with another
#![allow(unused)] fn main() { let x = Bits::from([0x0A, 0x0B, 0x0C]); let y = Bits::from([0x09, 0x0B, 0x0C]); let z = Bits::from([0x0A, 0x09, 0x0D]); assert_eq!(x.partial_cmp(&y), Some(Ordering::Greater)); assert_eq!(x.partial_cmp(&z), Some(Ordering::Less)); }
#![allow(unused)] fn main() { fn lt(&self, other: &Rhs) -> bool }Tests less than (for self and other) and is used by the < operator.
#![allow(unused)] fn main() { fn le(&self, other: &Rhs) -> bool }Tests less than or equal to (for self and other) and is used by the <= operator.
#![allow(unused)] fn main() { fn gt(&self, other: &Rhs) -> bool }Tests greater than (for self and other) and is used by the > operator.
#![allow(unused)] fn main() { fn ge(&self, other: &Rhs) -> bool }Tests greater than or equal to (for self and other) and is used by the >= operator.
Shl
#![allow(unused)] fn main() { fn shl(&self, count: &usize) -> Bits fn shl(self, count: &usize) -> Bits fn shl(&self, count: usize) -> Bits fn shl(self, count: usize) -> Bits }Shifts out upper bits, shifting in zeros on the lower end.
#![allow(unused)] fn main() { type Output = Bits }The resulting type after applying the << operator.
#![allow(unused)] fn main() { let x1 = Bits::from([0x0A, 0x0B, 0x0C]); let s1 = &x1 << &17; assert_eq!(s1, Bits::slice(&[0x00, 0x00, 0x04], 20)); assert_eq!(s1.len(), x1.len()); let x2 = Bits::new([0x0A, 0x0B, 0x0C]); let s2 = &x2 << &4; assert_eq!(s2, Bits::new([0xA0, 0xB0, 0xC0])); assert_eq!(s2.len(), x2.len()); }
ShlAssign
#![allow(unused)] fn main() { fn shl_assign(&mut self, count: &usize) fn shl_assign(&mut self, count: usize) }Shifts out upper bits, shifting in zeros on the lower end.
#![allow(unused)] fn main() { let mut x1 = Bits::from([0x0A, 0x0B, 0x0C]); x1 <<= &17; assert_eq!(x1, Bits::slice(&[0x00, 0x00, 0x04], 20)); assert_eq!(x1.len(), 20); let mut x2 = Bits::new([0x0A, 0x0B, 0x0C]); x2 <<= &4; assert_eq!(x2, Bits::new([0xA0, 0xB0, 0xC0])); assert_eq!(x2.len(), 24); }
Shr
#![allow(unused)] fn main() { fn shr(&self, count: &usize) -> Bits fn shr(self, count: &usize) -> Bits fn shr(&self, count: usize) -> Bits fn shr(self, count: usize) -> Bits }Shifts out lower bits, shifting zeros into the upper bits.
#![allow(unused)] fn main() { type Output = Bits }The resulting type after applying the >> operator.
#![allow(unused)] fn main() { let x1 = Bits::from([0x0A, 0x0B, 0x0C]); let s1 = &x1 >> &17; assert_eq!(s1.len(), x1.len()); assert_eq!(s1, Bits::slice(&[0x06, 0x00, 0x00], 20)); let x2 = Bits::new([0x0A, 0x0B, 0x0C]); let s2 = &x2 >> &4; assert_eq!(s2.len(), 24); assert_eq!(s2, Bits::new([0xB0, 0xC0, 0x00])); }
ShrAssign
#![allow(unused)] fn main() { fn shr_assign(&mut self, count: &usize) fn shr_assign(&mut self, count: usize) }Shifts out lower bits, shifting zeros into the upper bits.
#![allow(unused)] fn main() { let mut x1 = Bits::from([0x0A, 0x0B, 0x0C]); x1 >>= &17; assert_eq!(x1.len(), 20); assert_eq!(x1, Bits::slice(&[0x06, 0x00, 0x00], 20)); let mut x2 = Bits::new([0x0A, 0x0B, 0x0C]); x2 >>= &4; assert_eq!(x2.len(), 24); assert_eq!(x2, Bits::new([0xB0, 0xC0, 0x00])); }
Eq
Send
Sync
IntoBits
Iterator over individual bits of a bit string.
#![allow(unused)] fn main() { pub struct IntoBits { ... } }
impl IntoBits
#![allow(unused)] fn main() { pub fn skip_bits_while<P>(&mut self, f: P) -> usize where P: FnMut(u8) -> bool }Moves the iterator ahead until the predicate returns true.
This method behaves like
Iterator::skip_while, but doesn’t rely on repeated calls toIterator::next.
#![allow(unused)] fn main() { pub fn skip_bits(&mut self, n: usize) }Moves the iterator ahead.
This method behaves like
Iterator::skip, but doesn’t rely on repeated calls toIterator::next.
impl DoubleEndedIterator for IntoBits
#![allow(unused)] fn main() { fn next_back(&mut self) -> Option<Self::Item> }Removes and returns an element from the end of the iterator.
#![allow(unused)] fn main() { fn nth_back(&mut self, n: usize) -> Option<Self::Item> }Returns the nth element from the end of the iterator.
#![allow(unused)] fn main() { fn advance_back_by(&mut self, n: usize) -> Result<(), NonZero<usize>> }🔬This is a nightly-only experimental API. (iter_advance_by)
Advances the iterator from the back by n elements.
#![allow(unused)] fn main() { fn try_rfold<B, F, R>(&mut self, init: B, f: F) -> R where Self: Sized, F: FnMut(B, Self::Item) -> R, R: Try<Output = B> }This is the reverse version of
Iterator::try_fold(): it takes elements starting from the back of the iterator.
#![allow(unused)] fn main() { fn rfold<B, F>(self, init: B, f: F) -> B where Self: Sized, F: FnMut(B, Self::Item) -> B }An iterator method that reduces the iterator’s elements to a single, final value, starting from the back.
#![allow(unused)] fn main() { fn rfind<P>(&mut self, predicate: P) -> Option<Self::Item> where Self: Sized, P: FnMut(&Self::Item) -> bool }Searches for an element of an iterator from the back that satisfies a predicate.
impl ExactSizeIterator for IntoBits
#![allow(unused)] fn main() { fn len(&self) -> usize }Returns the exact remaining length of the iterator.
#![allow(unused)] fn main() { fn is_empty(&self) -> bool }🔬This is a nightly-only experimental API. (exact_size_is_empty)
Returns true if the iterator is empty.
impl Iterator for IntoBits
#![allow(unused)] fn main() { type Item = u8 }The type of the elements being iterated over.
#![allow(unused)] fn main() { fn next(&mut self) -> Option<Self::Item> }Advances the iterator and returns the next value.
#![allow(unused)] fn main() { fn size_hint(&self) -> (usize, Option<usize>) }Returns the bounds on the remaining length of the iterator.
#![allow(unused)] fn main() { fn count(self) -> usize }Consumes the iterator, counting the number of iterations and returning it.
#![allow(unused)] fn main() { fn last(self) -> Option<u8> }Consumes the iterator, returning the last element.
#![allow(unused)] fn main() { fn nth(&mut self, n: usize) -> Option<u8> }Returns the nth element of the iterator.
#![allow(unused)] fn main() { fn next_chunk<const N: usize>(&mut self) -> Result<[Self::Item; N], IntoIter<Self::Item, N>> where Self: Sized }🔬This is a nightly-only experimental API. (iter_next_chunk)
Advances the iterator and returns an array containing the next N values.
#![allow(unused)] fn main() { fn advance_by(&mut self, n: usize) -> Result<(), NonZero<usize>> }🔬This is a nightly-only experimental API. (iter_advance_by)
Advances the iterator by n elements.
#![allow(unused)] fn main() { fn step_by(self, step: usize) -> StepBy<Self> where Self: Sized }Creates an iterator starting at the same point, but stepping by the given amount at each iteration.
#![allow(unused)] fn main() { fn chain<U>(self, other: U) -> Chain<Self, <U as IntoIterator>::IntoIter> where Self: Sized, U: IntoIterator<Item = Self::Item> }Takes two iterators and creates a new iterator over both in sequence.
#![allow(unused)] fn main() { fn zip<U>(self, other: U) -> Zip<Self, <U as IntoIterator>::IntoIter> where Self: Sized, U: IntoIterator }‘Zips up’ two iterators into a single iterator of pairs.
#![allow(unused)] fn main() { fn intersperse(self, separator: Self::Item) -> Intersperse<Self> where Self: Sized, Self::Item: Clone }🔬This is a nightly-only experimental API. (iter_intersperse)
Creates a new iterator which places a copy of separator between adjacent items of the original iterator.
#![allow(unused)] fn main() { fn intersperse_with<G>(self, separator: G) -> IntersperseWith<Self, G> where Self: Sized, G: FnMut() -> Self::Item }🔬This is a nightly-only experimental API. (iter_intersperse)
Creates a new iterator which places an item generated by separator between adjacent items of the original iterator.
#![allow(unused)] fn main() { fn map<B, F>(self, f: F) -> Map<Self, F> where Self: Sized, F: FnMut(Self::Item) -> B }Takes a closure and creates an iterator which calls that closure on each element.
#![allow(unused)] fn main() { fn for_each<F>(self, f: F) where Self: Sized, F: FnMut(Self::Item) }Calls a closure on each element of an iterator.
#![allow(unused)] fn main() { fn filter<P>(self, predicate: P) -> Filter<Self, P> where Self: Sized, P: FnMut(&Self::Item) -> bool }Creates an iterator which uses a closure to determine if an element should be yielded.
#![allow(unused)] fn main() { fn filter_map<B, F>(self, f: F) -> FilterMap<Self, F> where Self: Sized, F: FnMut(Self::Item) -> Option<B> }Creates an iterator that both filters and maps.
#![allow(unused)] fn main() { fn enumerate(self) -> Enumerate<Self> where Self: Sized }Creates an iterator which gives the current iteration count as well as the next value.
#![allow(unused)] fn main() { fn peekable(self) -> Peekable<Self> where Self: Sized }Creates an iterator which can use the peek and peek_mut methods to look at the next element of the iterator without consuming it. See their documentation for more information.
#![allow(unused)] fn main() { fn skip_while<P>(self, predicate: P) -> SkipWhile<Self, P> where Self: Sized, P: FnMut(&Self::Item) -> bool }Creates an iterator that skips elements based on a predicate.
#![allow(unused)] fn main() { fn take_while<P>(self, predicate: P) -> TakeWhile<Self, P> where Self: Sized, P: FnMut(&Self::Item) -> bool }Creates an iterator that yields elements based on a predicate.
#![allow(unused)] fn main() { fn map_while<B, P>(self, predicate: P) -> MapWhile<Self, P> where Self: Sized, P: FnMut(Self::Item) -> Option<B> }Creates an iterator that both yields elements based on a predicate and maps.
#![allow(unused)] fn main() { fn skip(self, n: usize) -> Skip<Self> where Self: Sized }Creates an iterator that skips the first n elements.
#![allow(unused)] fn main() { fn take(self, n: usize) -> Take<Self> where Self: Sized }Creates an iterator that yields the first n elements, or fewer if the underlying iterator ends sooner.
#![allow(unused)] fn main() { fn scan<St, B, F>(self, initial_state: St, f: F) -> Scan<Self, St, F> where Self: Sized, F: FnMut(&mut St, Self::Item) -> Option<B> }An iterator adapter which, like fold, holds internal state, but unlike fold, produces a new iterator.
#![allow(unused)] fn main() { fn flat_map<U, F>(self, f: F) -> FlatMap<Self, U, F> where Self: Sized, U: IntoIterator, F: FnMut(Self::Item) -> U }Creates an iterator that works like map, but flattens nested structure.
#![allow(unused)] fn main() { fn flatten(self) -> Flatten<Self> where Self: Sized, Self::Item: IntoIterator }Creates an iterator that flattens nested structure.
#![allow(unused)] fn main() { fn map_windows<F, R, const N: usize>(self, f: F) -> MapWindows<Self, F, N> where Self: Sized, F: FnMut(&[Self::Item; N]) -> R }🔬This is a nightly-only experimental API. (iter_map_windows)
Calls the given function f for each contiguous window of size N over self and returns an iterator over the outputs of f. Like slice::windows(), the windows during mapping overlap as well.
#![allow(unused)] fn main() { fn fuse(self) -> Fuse<Self> where Self: Sized }Creates an iterator which ends after the first None.
#![allow(unused)] fn main() { fn inspect<F>(self, f: F) -> Inspect<Self, F> where Self: Sized, F: FnMut(&Self::Item) }Does something with each element of an iterator, passing the value on.
#![allow(unused)] fn main() { fn by_ref(&mut self) -> &mut Self where Self: Sized }Borrows an iterator, rather than consuming it.
#![allow(unused)] fn main() { fn collect<B>(self) -> B where B: FromIterator<Self::Item>, Self: Sized }Transforms an iterator into a collection.
#![allow(unused)] fn main() { fn try_collect<B>(&mut self,) -> <<Self::Item as Try>::Residual as Residual<B>>::TryType where Self: Sized, Self::Item: Try, <Self::Item as Try>::Residual: Residual<B>, B: FromIterator<<Self::Item as Try>::Output> }🔬This is a nightly-only experimental API. (iterator_try_collect)
Fallibly transforms an iterator into a collection, short circuiting if a failure is encountered.
#![allow(unused)] fn main() { fn collect_into<E>(self, collection: &mut E) -> &mut E where E: Extend<Self::Item>, Self: Sized }🔬This is a nightly-only experimental API. (iter_collect_into)
Collects all the items from an iterator into a collection.
#![allow(unused)] fn main() { fn partition<B, F>(self, f: F) -> (B, B) where Self: Sized, B: Default + Extend<Self::Item>, F: FnMut(&Self::Item) -> bool }Consumes an iterator, creating two collections from it.
#![allow(unused)] fn main() { fn partition_in_place<'a, T, P>(self, predicate: P) -> usize where T: 'a, Self: Sized + DoubleEndedIterator<Item = &'a mut T>, P: FnMut(&T) -> bool }🔬This is a nightly-only experimental API. (iter_partition_in_place)
Reorders the elements of this iterator in-place according to the given predicate, such that all those that return true precede all those that return false. Returns the number of true elements found.
#![allow(unused)] fn main() { fn is_partitioned<P>(self, predicate: P) -> bool where Self: Sized, P: FnMut(Self::Item) -> bool }🔬This is a nightly-only experimental API. (iter_is_partitioned)
Checks if the elements of this iterator are partitioned according to the given predicate, such that all those that return true precede all those that return false.
#![allow(unused)] fn main() { fn try_fold<B, F, R>(&mut self, init: B, f: F) -> R where Self: Sized, F: FnMut(B, Self::Item) -> R, R: Try<Output = B> }An iterator method that applies a function as long as it returns successfully, producing a single, final value.
#![allow(unused)] fn main() { fn try_for_each<F, R>(&mut self, f: F) -> R where Self: Sized, F: FnMut(Self::Item) -> R, R: Try<Output = ()> }An iterator method that applies a fallible function to each item in the iterator, stopping at the first error and returning that error.
#![allow(unused)] fn main() { fn fold<B, F>(self, init: B, f: F) -> B where Self: Sized, F: FnMut(B, Self::Item) -> B }Folds every element into an accumulator by applying an operation, returning the final result.
#![allow(unused)] fn main() { fn reduce<F>(self, f: F) -> Option<Self::Item> where Self: Sized, F: FnMut(Self::Item, Self::Item) -> Self::Item }Reduces the elements to a single one, by repeatedly applying a reducing operation.
#![allow(unused)] fn main() { fn try_reduce<R>( &mut self, f: impl FnMut(Self::Item, Self::Item) -> R, ) -> <<R as Try>::Residual as Residual<Option<<R as Try>::Output>>>::TryType where Self: Sized, R: Try<Output = Self::Item>, <R as Try>::Residual: Residual<Option<Self::Item>> }🔬This is a nightly-only experimental API. (iterator_try_reduce)
Reduces the elements to a single one by repeatedly applying a reducing operation. If the closure returns a failure, the failure is propagated back to the caller immediately.
#![allow(unused)] fn main() { fn all<F>(&mut self, f: F) -> bool where Self: Sized, F: FnMut(Self::Item) -> bool }Tests if every element of the iterator matches a predicate.
#![allow(unused)] fn main() { fn any<F>(&mut self, f: F) -> bool where Self: Sized, F: FnMut(Self::Item) -> bool }Tests if any element of the iterator matches a predicate.
#![allow(unused)] fn main() { fn find<P>(&mut self, predicate: P) -> Option<Self::Item> where Self: Sized, P: FnMut(&Self::Item) -> bool }Searches for an element of an iterator that satisfies a predicate.
#![allow(unused)] fn main() { fn find_map<B, F>(&mut self, f: F) -> Option<B> where Self: Sized, F: FnMut(Self::Item) -> Option<B> }Applies function to the elements of iterator and returns the first non-none result.
#![allow(unused)] fn main() { fn try_find<R>( &mut self, f: impl FnMut(&Self::Item) -> R, ) -> <<R as Try>::Residual as Residual<Option<Self::Item>>>::TryType where Self: Sized, R: Try<Output = bool>, <R as Try>::Residual: Residual<Option<Self::Item>> }🔬This is a nightly-only experimental API. (try_find)
Applies function to the elements of iterator and returns the first true result or the first error.
#![allow(unused)] fn main() { fn position<P>(&mut self, predicate: P) -> Option<usize> where Self: Sized, P: FnMut(Self::Item) -> bool }Searches for an element in an iterator, returning its index.
#![allow(unused)] fn main() { fn rposition<P>(&mut self, predicate: P) -> Option<usize> where P: FnMut(Self::Item) -> bool, Self: Sized + ExactSizeIterator + DoubleEndedIterator }Searches for an element in an iterator from the right, returning its index.
#![allow(unused)] fn main() { fn max(self) -> Option<Self::Item> where Self: Sized, Self::Item: Ord }Returns the maximum element of an iterator.
#![allow(unused)] fn main() { fn min(self) -> Option<Self::Item> where Self: Sized, Self::Item: Ord }Returns the minimum element of an iterator.
#![allow(unused)] fn main() { fn max_by_key<B, F>(self, f: F) -> Option<Self::Item> where B: Ord, Self: Sized, F: FnMut(&Self::Item) -> B }Returns the element that gives the maximum value from the specified function.
#![allow(unused)] fn main() { fn max_by<F>(self, compare: F) -> Option<Self::Item> where Self: Sized, F: FnMut(&Self::Item, &Self::Item) -> Ordering }Returns the element that gives the maximum value with respect to the specified comparison function.
#![allow(unused)] fn main() { fn min_by_key<B, F>(self, f: F) -> Option<Self::Item> where B: Ord, Self: Sized, F: FnMut(&Self::Item) -> B }Returns the element that gives the minimum value from the specified function.
#![allow(unused)] fn main() { fn min_by<F>(self, compare: F) -> Option<Self::Item> where Self: Sized, F: FnMut(&Self::Item, &Self::Item) -> Ordering }Returns the element that gives the minimum value with respect to the specified comparison function.
#![allow(unused)] fn main() { fn rev(self) -> Rev<Self> where Self: Sized + DoubleEndedIterator }Reverses an iterator’s direction.
#![allow(unused)] fn main() { fn unzip<A, B, FromA, FromB>(self) -> (FromA, FromB) where FromA: Default + Extend<A>, FromB: Default + Extend<B>, Self: Sized + Iterator<Item = (A, B)> }Converts an iterator of pairs into a pair of containers.
#![allow(unused)] fn main() { fn copied<'a, T>(self) -> Copied<Self> where T: 'a + Copy, Self: Sized + Iterator<Item = &'a T> }Creates an iterator which copies all of its elements.
#![allow(unused)] fn main() { fn cloned<'a, T>(self) -> Cloned<Self> where T: 'a + Clone, Self: Sized + Iterator<Item = &'a T> }Creates an iterator which clones all of its elements.
#![allow(unused)] fn main() { fn array_chunks<const N: usize>(self) -> ArrayChunks<Self, N> where Self: Sized }🔬This is a nightly-only experimental API. (iter_array_chunks)
Returns an iterator over N elements of the iterator at a time.
#![allow(unused)] fn main() { fn sum<S>(self) -> S where Self: Sized, S: Sum<Self::Item> }Sums the elements of an iterator.
#![allow(unused)] fn main() { fn product<P>(self) -> P where Self: Sized, P: Product<Self::Item> }Iterates over the entire iterator, multiplying all the elements
#![allow(unused)] fn main() { fn cmp<I>(self, other: I) -> Ordering where I: IntoIterator<Item = Self::Item>, Self::Item: Ord, Self: Sized }Lexicographically compares the elements of this Iterator with those of another.
#![allow(unused)] fn main() { fn cmp_by<I, F>(self, other: I, cmp: F) -> Ordering where Self: Sized, I: IntoIterator, F: FnMut(Self::Item, <I as IntoIterator>::Item) -> Ordering }🔬This is a nightly-only experimental API. (iter_order_by)
Lexicographically compares the elements of this Iterator with those of another with respect to the specified comparison function.
#![allow(unused)] fn main() { fn partial_cmp<I>(self, other: I) -> Option<Ordering> where I: IntoIterator, Self::Item: PartialOrd<<I as IntoIterator>::Item>, Self: Sized }Lexicographically compares the PartialOrd elements of this Iterator with those of another. The comparison works like short-circuit evaluation, returning a result without comparing the remaining elements. As soon as an order can be determined, the evaluation stops and a result is returned.
#![allow(unused)] fn main() { fn partial_cmp_by<I, F>(self, other: I, partial_cmp: F) -> Option<Ordering> where Self: Sized, I: IntoIterator, F: FnMut(Self::Item, <I as IntoIterator>::Item) -> Option<Ordering> }🔬This is a nightly-only experimental API. (iter_order_by)
Lexicographically compares the elements of this Iterator with those of another with respect to the specified comparison function.
#![allow(unused)] fn main() { fn eq<I>(self, other: I) -> bool where I: IntoIterator, Self::Item: PartialEq<<I as IntoIterator>::Item>, Self: Sized }Determines if the elements of this Iterator are equal to those of another.
#![allow(unused)] fn main() { fn eq_by<I, F>(self, other: I, eq: F) -> bool where Self: Sized, I: IntoIterator, F: FnMut(Self::Item, <I as IntoIterator>::Item) -> bool }🔬This is a nightly-only experimental API. (iter_order_by)
Determines if the elements of this Iterator are equal to those of another with respect to the specified equality function.
#![allow(unused)] fn main() { fn ne<I>(self, other: I) -> bool where I: IntoIterator, Self::Item: PartialEq<<I as IntoIterator>::Item>, Self: Sized }Determines if the elements of this Iterator are not equal to those of another.
#![allow(unused)] fn main() { fn lt<I>(self, other: I) -> bool where I: IntoIterator, Self::Item: PartialOrd<<I as IntoIterator>::Item>, Self: Sized }Determines if the elements of this Iterator are lexicographically less than those of another.
#![allow(unused)] fn main() { fn le<I>(self, other: I) -> bool where I: IntoIterator, Self::Item: PartialOrd<<I as IntoIterator>::Item>, Self: Sized }Determines if the elements of this Iterator are lexicographically less or equal to those of another.
#![allow(unused)] fn main() { fn gt<I>(self, other: I) -> bool where I: IntoIterator, Self::Item: PartialOrd<<I as IntoIterator>::Item>, Self: Sized }Determines if the elements of this Iterator are lexicographically greater than those of another.
#![allow(unused)] fn main() { fn ge<I>(self, other: I) -> bool where I: IntoIterator, Self::Item: PartialOrd<<I as IntoIterator>::Item>, Self: Sized }Determines if the elements of this Iterator are lexicographically greater than or equal to those of another.
#![allow(unused)] fn main() { fn is_sorted(self) -> bool where Self: Sized, Self::Item: PartialOrd }Checks if the elements of this iterator are sorted.
#![allow(unused)] fn main() { fn is_sorted_by<F>(self, compare: F) -> bool where Self: Sized, F: FnMut(&Self::Item, &Self::Item) -> bool }Checks if the elements of this iterator are sorted using the given comparator function.
#![allow(unused)] fn main() { fn is_sorted_by_key<F, K>(self, f: F) -> bool where Self: Sized, F: FnMut(Self::Item) -> K, K: PartialOrd }Checks if the elements of this iterator are sorted using the given key extraction function.
impl FusedIterator for IntoBits
IntoBytes
Iterator over individual bytes of a bit string.
#![allow(unused)] fn main() { pub struct IntoBytes { ... } }
impl DoubleEndedIterator for IntoBytes
#![allow(unused)] fn main() { fn next_back(&mut self) -> Option<Self::Item> }Removes and returns an element from the end of the iterator.
#![allow(unused)] fn main() { fn nth_back(&mut self, n: usize) -> Option<Self::Item> }Returns the nth element from the end of the iterator.
#![allow(unused)] fn main() { fn advance_back_by(&mut self, n: usize) -> Result<(), NonZero<usize>> }🔬This is a nightly-only experimental API. (iter_advance_by)
Advances the iterator from the back by n elements.
#![allow(unused)] fn main() { fn try_rfold<B, F, R>(&mut self, init: B, f: F) -> R where Self: Sized, F: FnMut(B, Self::Item) -> R, R: Try<Output = B> }This is the reverse version of
Iterator::try_fold(): it takes elements starting from the back of the iterator.
#![allow(unused)] fn main() { fn rfold<B, F>(self, init: B, f: F) -> B where Self: Sized, F: FnMut(B, Self::Item) -> B }An iterator method that reduces the iterator’s elements to a single, final value, starting from the back.
#![allow(unused)] fn main() { fn rfind<P>(&mut self, predicate: P) -> Option<Self::Item> where Self: Sized, P: FnMut(&Self::Item) -> bool }Searches for an element of an iterator from the back that satisfies a predicate.
impl ExactSizeIterator for IntoBytes
#![allow(unused)] fn main() { fn len(&self) -> usize }Returns the exact remaining length of the iterator.
#![allow(unused)] fn main() { fn is_empty(&self) -> bool }🔬This is a nightly-only experimental API. (exact_size_is_empty)
Returns true if the iterator is empty.
impl Iterator for IntoBytes
#![allow(unused)] fn main() { type Item = u8 }The type of the elements being iterated over.
#![allow(unused)] fn main() { fn next(&mut self) -> Option<Self::Item> }Advances the iterator and returns the next value.
#![allow(unused)] fn main() { fn size_hint(&self) -> (usize, Option<usize>) }Returns the bounds on the remaining length of the iterator.
#![allow(unused)] fn main() { fn count(self) -> usize }Consumes the iterator, counting the number of iterations and returning it.
#![allow(unused)] fn main() { fn last(self) -> Option<u8> }Consumes the iterator, returning the last element.
#![allow(unused)] fn main() { fn nth(&mut self, n: usize) -> Option<u8> }Returns the nth element of the iterator.
#![allow(unused)] fn main() { fn next_chunk<const N: usize>(&mut self) -> Result<[Self::Item; N], IntoIter<Self::Item, N>> where Self: Sized }🔬This is a nightly-only experimental API. (iter_next_chunk)
Advances the iterator and returns an array containing the next N values.
#![allow(unused)] fn main() { fn advance_by(&mut self, n: usize) -> Result<(), NonZero<usize>> }🔬This is a nightly-only experimental API. (iter_advance_by)
Advances the iterator by n elements.
#![allow(unused)] fn main() { fn step_by(self, step: usize) -> StepBy<Self> where Self: Sized }Creates an iterator starting at the same point, but stepping by the given amount at each iteration.
#![allow(unused)] fn main() { fn chain<U>(self, other: U) -> Chain<Self, <U as IntoIterator>::IntoIter> where Self: Sized, U: IntoIterator<Item = Self::Item> }Takes two iterators and creates a new iterator over both in sequence.
#![allow(unused)] fn main() { fn zip<U>(self, other: U) -> Zip<Self, <U as IntoIterator>::IntoIter> where Self: Sized, U: IntoIterator }‘Zips up’ two iterators into a single iterator of pairs.
#![allow(unused)] fn main() { fn intersperse(self, separator: Self::Item) -> Intersperse<Self> where Self: Sized, Self::Item: Clone }🔬This is a nightly-only experimental API. (iter_intersperse)
Creates a new iterator which places a copy of separator between adjacent items of the original iterator.
#![allow(unused)] fn main() { fn intersperse_with<G>(self, separator: G) -> IntersperseWith<Self, G> where Self: Sized, G: FnMut() -> Self::Item }🔬This is a nightly-only experimental API. (iter_intersperse)
Creates a new iterator which places an item generated by separator between adjacent items of the original iterator.
#![allow(unused)] fn main() { fn map<B, F>(self, f: F) -> Map<Self, F> where Self: Sized, F: FnMut(Self::Item) -> B }Takes a closure and creates an iterator which calls that closure on each element.
#![allow(unused)] fn main() { fn for_each<F>(self, f: F) where Self: Sized, F: FnMut(Self::Item) }Calls a closure on each element of an iterator.
#![allow(unused)] fn main() { fn filter<P>(self, predicate: P) -> Filter<Self, P> where Self: Sized, P: FnMut(&Self::Item) -> bool }Creates an iterator which uses a closure to determine if an element should be yielded.
#![allow(unused)] fn main() { fn filter_map<B, F>(self, f: F) -> FilterMap<Self, F> where Self: Sized, F: FnMut(Self::Item) -> Option<B> }Creates an iterator that both filters and maps.
#![allow(unused)] fn main() { fn enumerate(self) -> Enumerate<Self> where Self: Sized }Creates an iterator which gives the current iteration count as well as the next value.
#![allow(unused)] fn main() { fn peekable(self) -> Peekable<Self> where Self: Sized }Creates an iterator which can use the peek and peek_mut methods to look at the next element of the iterator without consuming it. See their documentation for more information.
#![allow(unused)] fn main() { fn skip_while<P>(self, predicate: P) -> SkipWhile<Self, P> where Self: Sized, P: FnMut(&Self::Item) -> bool }Creates an iterator that skips elements based on a predicate.
#![allow(unused)] fn main() { fn take_while<P>(self, predicate: P) -> TakeWhile<Self, P> where Self: Sized, P: FnMut(&Self::Item) -> bool }Creates an iterator that yields elements based on a predicate.
#![allow(unused)] fn main() { fn map_while<B, P>(self, predicate: P) -> MapWhile<Self, P> where Self: Sized, P: FnMut(Self::Item) -> Option<B> }Creates an iterator that both yields elements based on a predicate and maps.
#![allow(unused)] fn main() { fn skip(self, n: usize) -> Skip<Self> where Self: Sized }Creates an iterator that skips the first n elements.
#![allow(unused)] fn main() { fn take(self, n: usize) -> Take<Self> where Self: Sized }Creates an iterator that yields the first n elements, or fewer if the underlying iterator ends sooner.
#![allow(unused)] fn main() { fn scan<St, B, F>(self, initial_state: St, f: F) -> Scan<Self, St, F> where Self: Sized, F: FnMut(&mut St, Self::Item) -> Option<B> }An iterator adapter which, like fold, holds internal state, but unlike fold, produces a new iterator.
#![allow(unused)] fn main() { fn flat_map<U, F>(self, f: F) -> FlatMap<Self, U, F> where Self: Sized, U: IntoIterator, F: FnMut(Self::Item) -> U }Creates an iterator that works like map, but flattens nested structure.
#![allow(unused)] fn main() { fn flatten(self) -> Flatten<Self> where Self: Sized, Self::Item: IntoIterator }Creates an iterator that flattens nested structure.
#![allow(unused)] fn main() { fn map_windows<F, R, const N: usize>(self, f: F) -> MapWindows<Self, F, N> where Self: Sized, F: FnMut(&[Self::Item; N]) -> R }🔬This is a nightly-only experimental API. (iter_map_windows)
Calls the given function f for each contiguous window of size N over self and returns an iterator over the outputs of f. Like slice::windows(), the windows during mapping overlap as well.
#![allow(unused)] fn main() { fn fuse(self) -> Fuse<Self> where Self: Sized }Creates an iterator which ends after the first None.
#![allow(unused)] fn main() { fn inspect<F>(self, f: F) -> Inspect<Self, F> where Self: Sized, F: FnMut(&Self::Item) }Does something with each element of an iterator, passing the value on.
#![allow(unused)] fn main() { fn by_ref(&mut self) -> &mut Self where Self: Sized }Borrows an iterator, rather than consuming it.
#![allow(unused)] fn main() { fn collect<B>(self) -> B where B: FromIterator<Self::Item>, Self: Sized }Transforms an iterator into a collection.
#![allow(unused)] fn main() { fn try_collect<B>(&mut self,) -> <<Self::Item as Try>::Residual as Residual<B>>::TryType where Self: Sized, Self::Item: Try, <Self::Item as Try>::Residual: Residual<B>, B: FromIterator<<Self::Item as Try>::Output> }🔬This is a nightly-only experimental API. (iterator_try_collect)
Fallibly transforms an iterator into a collection, short circuiting if a failure is encountered.
#![allow(unused)] fn main() { fn collect_into<E>(self, collection: &mut E) -> &mut E where E: Extend<Self::Item>, Self: Sized }🔬This is a nightly-only experimental API. (iter_collect_into)
Collects all the items from an iterator into a collection.
#![allow(unused)] fn main() { fn partition<B, F>(self, f: F) -> (B, B) where Self: Sized, B: Default + Extend<Self::Item>, F: FnMut(&Self::Item) -> bool }Consumes an iterator, creating two collections from it.
#![allow(unused)] fn main() { fn partition_in_place<'a, T, P>(self, predicate: P) -> usize where T: 'a, Self: Sized + DoubleEndedIterator<Item = &'a mut T>, P: FnMut(&T) -> bool }🔬This is a nightly-only experimental API. (iter_partition_in_place)
Reorders the elements of this iterator in-place according to the given predicate, such that all those that return true precede all those that return false. Returns the number of true elements found.
#![allow(unused)] fn main() { fn is_partitioned<P>(self, predicate: P) -> bool where Self: Sized, P: FnMut(Self::Item) -> bool }🔬This is a nightly-only experimental API. (iter_is_partitioned)
Checks if the elements of this iterator are partitioned according to the given predicate, such that all those that return true precede all those that return false.
#![allow(unused)] fn main() { fn try_fold<B, F, R>(&mut self, init: B, f: F) -> R where Self: Sized, F: FnMut(B, Self::Item) -> R, R: Try<Output = B> }An iterator method that applies a function as long as it returns successfully, producing a single, final value.
#![allow(unused)] fn main() { fn try_for_each<F, R>(&mut self, f: F) -> R where Self: Sized, F: FnMut(Self::Item) -> R, R: Try<Output = ()> }An iterator method that applies a fallible function to each item in the iterator, stopping at the first error and returning that error.
#![allow(unused)] fn main() { fn fold<B, F>(self, init: B, f: F) -> B where Self: Sized, F: FnMut(B, Self::Item) -> B }Folds every element into an accumulator by applying an operation, returning the final result.
#![allow(unused)] fn main() { fn reduce<F>(self, f: F) -> Option<Self::Item> where Self: Sized, F: FnMut(Self::Item, Self::Item) -> Self::Item }Reduces the elements to a single one, by repeatedly applying a reducing operation.
#![allow(unused)] fn main() { fn try_reduce<R>( &mut self, f: impl FnMut(Self::Item, Self::Item) -> R, ) -> <<R as Try>::Residual as Residual<Option<<R as Try>::Output>>>::TryType where Self: Sized, R: Try<Output = Self::Item>, <R as Try>::Residual: Residual<Option<Self::Item>> }🔬This is a nightly-only experimental API. (iterator_try_reduce)
Reduces the elements to a single one by repeatedly applying a reducing operation. If the closure returns a failure, the failure is propagated back to the caller immediately.
#![allow(unused)] fn main() { fn all<F>(&mut self, f: F) -> bool where Self: Sized, F: FnMut(Self::Item) -> bool }Tests if every element of the iterator matches a predicate.
#![allow(unused)] fn main() { fn any<F>(&mut self, f: F) -> bool where Self: Sized, F: FnMut(Self::Item) -> bool }Tests if any element of the iterator matches a predicate.
#![allow(unused)] fn main() { fn find<P>(&mut self, predicate: P) -> Option<Self::Item> where Self: Sized, P: FnMut(&Self::Item) -> bool }Searches for an element of an iterator that satisfies a predicate.
#![allow(unused)] fn main() { fn find_map<B, F>(&mut self, f: F) -> Option<B> where Self: Sized, F: FnMut(Self::Item) -> Option<B> }Applies function to the elements of iterator and returns the first non-none result.
#![allow(unused)] fn main() { fn try_find<R>( &mut self, f: impl FnMut(&Self::Item) -> R, ) -> <<R as Try>::Residual as Residual<Option<Self::Item>>>::TryType where Self: Sized, R: Try<Output = bool>, <R as Try>::Residual: Residual<Option<Self::Item>> }🔬This is a nightly-only experimental API. (try_find)
Applies function to the elements of iterator and returns the first true result or the first error.
#![allow(unused)] fn main() { fn position<P>(&mut self, predicate: P) -> Option<usize> where Self: Sized, P: FnMut(Self::Item) -> bool }Searches for an element in an iterator, returning its index.
#![allow(unused)] fn main() { fn rposition<P>(&mut self, predicate: P) -> Option<usize> where P: FnMut(Self::Item) -> bool, Self: Sized + ExactSizeIterator + DoubleEndedIterator }Searches for an element in an iterator from the right, returning its index.
#![allow(unused)] fn main() { fn max(self) -> Option<Self::Item> where Self: Sized, Self::Item: Ord }Returns the maximum element of an iterator.
#![allow(unused)] fn main() { fn min(self) -> Option<Self::Item> where Self: Sized, Self::Item: Ord }Returns the minimum element of an iterator.
#![allow(unused)] fn main() { fn max_by_key<B, F>(self, f: F) -> Option<Self::Item> where B: Ord, Self: Sized, F: FnMut(&Self::Item) -> B }Returns the element that gives the maximum value from the specified function.
#![allow(unused)] fn main() { fn max_by<F>(self, compare: F) -> Option<Self::Item> where Self: Sized, F: FnMut(&Self::Item, &Self::Item) -> Ordering }Returns the element that gives the maximum value with respect to the specified comparison function.
#![allow(unused)] fn main() { fn min_by_key<B, F>(self, f: F) -> Option<Self::Item> where B: Ord, Self: Sized, F: FnMut(&Self::Item) -> B }Returns the element that gives the minimum value from the specified function.
#![allow(unused)] fn main() { fn min_by<F>(self, compare: F) -> Option<Self::Item> where Self: Sized, F: FnMut(&Self::Item, &Self::Item) -> Ordering }Returns the element that gives the minimum value with respect to the specified comparison function.
#![allow(unused)] fn main() { fn rev(self) -> Rev<Self> where Self: Sized + DoubleEndedIterator }Reverses an iterator’s direction.
#![allow(unused)] fn main() { fn unzip<A, B, FromA, FromB>(self) -> (FromA, FromB) where FromA: Default + Extend<A>, FromB: Default + Extend<B>, Self: Sized + Iterator<Item = (A, B)> }Converts an iterator of pairs into a pair of containers.
#![allow(unused)] fn main() { fn copied<'a, T>(self) -> Copied<Self> where T: 'a + Copy, Self: Sized + Iterator<Item = &'a T> }Creates an iterator which copies all of its elements.
#![allow(unused)] fn main() { fn cloned<'a, T>(self) -> Cloned<Self> where T: 'a + Clone, Self: Sized + Iterator<Item = &'a T> }Creates an iterator which clones all of its elements.
#![allow(unused)] fn main() { fn array_chunks<const N: usize>(self) -> ArrayChunks<Self, N> where Self: Sized }🔬This is a nightly-only experimental API. (iter_array_chunks)
Returns an iterator over N elements of the iterator at a time.
#![allow(unused)] fn main() { fn sum<S>(self) -> S where Self: Sized, S: Sum<Self::Item> }Sums the elements of an iterator.
#![allow(unused)] fn main() { fn product<P>(self) -> P where Self: Sized, P: Product<Self::Item> }Iterates over the entire iterator, multiplying all the elements
#![allow(unused)] fn main() { fn cmp<I>(self, other: I) -> Ordering where I: IntoIterator<Item = Self::Item>, Self::Item: Ord, Self: Sized }Lexicographically compares the elements of this Iterator with those of another.
#![allow(unused)] fn main() { fn cmp_by<I, F>(self, other: I, cmp: F) -> Ordering where Self: Sized, I: IntoIterator, F: FnMut(Self::Item, <I as IntoIterator>::Item) -> Ordering }🔬This is a nightly-only experimental API. (iter_order_by)
Lexicographically compares the elements of this Iterator with those of another with respect to the specified comparison function.
#![allow(unused)] fn main() { fn partial_cmp<I>(self, other: I) -> Option<Ordering> where I: IntoIterator, Self::Item: PartialOrd<<I as IntoIterator>::Item>, Self: Sized }Lexicographically compares the PartialOrd elements of this Iterator with those of another. The comparison works like short-circuit evaluation, returning a result without comparing the remaining elements. As soon as an order can be determined, the evaluation stops and a result is returned.
#![allow(unused)] fn main() { fn partial_cmp_by<I, F>(self, other: I, partial_cmp: F) -> Option<Ordering> where Self: Sized, I: IntoIterator, F: FnMut(Self::Item, <I as IntoIterator>::Item) -> Option<Ordering> }🔬This is a nightly-only experimental API. (iter_order_by)
Lexicographically compares the elements of this Iterator with those of another with respect to the specified comparison function.
#![allow(unused)] fn main() { fn eq<I>(self, other: I) -> bool where I: IntoIterator, Self::Item: PartialEq<<I as IntoIterator>::Item>, Self: Sized }Determines if the elements of this Iterator are equal to those of another.
#![allow(unused)] fn main() { fn eq_by<I, F>(self, other: I, eq: F) -> bool where Self: Sized, I: IntoIterator, F: FnMut(Self::Item, <I as IntoIterator>::Item) -> bool }🔬This is a nightly-only experimental API. (iter_order_by)
Determines if the elements of this Iterator are equal to those of another with respect to the specified equality function.
#![allow(unused)] fn main() { fn ne<I>(self, other: I) -> bool where I: IntoIterator, Self::Item: PartialEq<<I as IntoIterator>::Item>, Self: Sized }Determines if the elements of this Iterator are not equal to those of another.
#![allow(unused)] fn main() { fn lt<I>(self, other: I) -> bool where I: IntoIterator, Self::Item: PartialOrd<<I as IntoIterator>::Item>, Self: Sized }Determines if the elements of this Iterator are lexicographically less than those of another.
#![allow(unused)] fn main() { fn le<I>(self, other: I) -> bool where I: IntoIterator, Self::Item: PartialOrd<<I as IntoIterator>::Item>, Self: Sized }Determines if the elements of this Iterator are lexicographically less or equal to those of another.
#![allow(unused)] fn main() { fn gt<I>(self, other: I) -> bool where I: IntoIterator, Self::Item: PartialOrd<<I as IntoIterator>::Item>, Self: Sized }Determines if the elements of this Iterator are lexicographically greater than those of another.
#![allow(unused)] fn main() { fn ge<I>(self, other: I) -> bool where I: IntoIterator, Self::Item: PartialOrd<<I as IntoIterator>::Item>, Self: Sized }Determines if the elements of this Iterator are lexicographically greater than or equal to those of another.
#![allow(unused)] fn main() { fn is_sorted(self) -> bool where Self: Sized, Self::Item: PartialOrd }Checks if the elements of this iterator are sorted.
#![allow(unused)] fn main() { fn is_sorted_by<F>(self, compare: F) -> bool where Self: Sized, F: FnMut(&Self::Item, &Self::Item) -> bool }Checks if the elements of this iterator are sorted using the given comparator function.
#![allow(unused)] fn main() { fn is_sorted_by_key<F, K>(self, f: F) -> bool where Self: Sized, F: FnMut(Self::Item) -> K, K: PartialOrd }Checks if the elements of this iterator are sorted using the given key extraction function.
impl FusedIterator for IntoBytes
Iter
#![allow(unused)] fn main() { pub struct Iter { ... } }
impl Iter
#![allow(unused)] fn main() { pub fn skip_bits_while<P>(&mut self, f: P) -> usize where P: FnMut(u8) -> bool }Moves the iterator ahead until the predicate returns true.
This method behaves like
Iterator::skip_while, but doesn’t rely on repeated calls toIterator::next.
#![allow(unused)] fn main() { pub fn skip_bits(&mut self, n: usize) }Moves the iterator ahead.
This method behaves like
Iterator::skip, but doesn’t rely on repeated calls toIterator::next.
impl DoubleEndedIterator for Iter
#![allow(unused)] fn main() { fn next_back(&mut self) -> Option<Self::Item> }Removes and returns an element from the end of the iterator.
#![allow(unused)] fn main() { fn nth_back(&mut self, n: usize) -> Option<Self::Item> }Returns the nth element from the end of the iterator.
#![allow(unused)] fn main() { fn advance_back_by(&mut self, n: usize) -> Result<(), NonZero<usize>> }🔬This is a nightly-only experimental API. (iter_advance_by)
Advances the iterator from the back by n elements.
#![allow(unused)] fn main() { fn try_rfold<B, F, R>(&mut self, init: B, f: F) -> R where Self: Sized, F: FnMut(B, Self::Item) -> R, R: Try<Output = B> }This is the reverse version of
Iterator::try_fold(): it takes elements starting from the back of the iterator.
#![allow(unused)] fn main() { fn rfold<B, F>(self, init: B, f: F) -> B where Self: Sized, F: FnMut(B, Self::Item) -> B }An iterator method that reduces the iterator’s elements to a single, final value, starting from the back.
#![allow(unused)] fn main() { fn rfind<P>(&mut self, predicate: P) -> Option<Self::Item> where Self: Sized, P: FnMut(&Self::Item) -> bool }Searches for an element of an iterator from the back that satisfies a predicate.
impl ExactSizeIterator for Iter
#![allow(unused)] fn main() { fn len(&self) -> usize }Returns the exact remaining length of the iterator.
#![allow(unused)] fn main() { fn is_empty(&self) -> bool }🔬This is a nightly-only experimental API. (exact_size_is_empty)
Returns true if the iterator is empty.
impl Iterator for Iter
#![allow(unused)] fn main() { type Item = u8 }The type of the elements being iterated over.
#![allow(unused)] fn main() { fn next(&mut self) -> Option<Self::Item> }Advances the iterator and returns the next value.
#![allow(unused)] fn main() { fn size_hint(&self) -> (usize, Option<usize>) }Returns the bounds on the remaining length of the iterator.
#![allow(unused)] fn main() { fn count(self) -> usize }Consumes the iterator, counting the number of iterations and returning it.
#![allow(unused)] fn main() { fn last(self) -> Option<u8> }Consumes the iterator, returning the last element.
#![allow(unused)] fn main() { fn nth(&mut self, n: usize) -> Option<u8> }Returns the nth element of the iterator.
#![allow(unused)] fn main() { fn next_chunk<const N: usize>(&mut self) -> Result<[Self::Item; N], IntoIter<Self::Item, N>> where Self: Sized }🔬This is a nightly-only experimental API. (iter_next_chunk)
Advances the iterator and returns an array containing the next N values.
#![allow(unused)] fn main() { fn advance_by(&mut self, n: usize) -> Result<(), NonZero<usize>> }🔬This is a nightly-only experimental API. (iter_advance_by)
Advances the iterator by n elements.
#![allow(unused)] fn main() { fn step_by(self, step: usize) -> StepBy<Self> where Self: Sized }Creates an iterator starting at the same point, but stepping by the given amount at each iteration.
#![allow(unused)] fn main() { fn chain<U>(self, other: U) -> Chain<Self, <U as IntoIterator>::IntoIter> where Self: Sized, U: IntoIterator<Item = Self::Item> }Takes two iterators and creates a new iterator over both in sequence.
#![allow(unused)] fn main() { fn zip<U>(self, other: U) -> Zip<Self, <U as IntoIterator>::IntoIter> where Self: Sized, U: IntoIterator }‘Zips up’ two iterators into a single iterator of pairs.
#![allow(unused)] fn main() { fn intersperse(self, separator: Self::Item) -> Intersperse<Self> where Self: Sized, Self::Item: Clone }🔬This is a nightly-only experimental API. (iter_intersperse)
Creates a new iterator which places a copy of separator between adjacent items of the original iterator.
#![allow(unused)] fn main() { fn intersperse_with<G>(self, separator: G) -> IntersperseWith<Self, G> where Self: Sized, G: FnMut() -> Self::Item }🔬This is a nightly-only experimental API. (iter_intersperse)
Creates a new iterator which places an item generated by separator between adjacent items of the original iterator.
#![allow(unused)] fn main() { fn map<B, F>(self, f: F) -> Map<Self, F> where Self: Sized, F: FnMut(Self::Item) -> B }Takes a closure and creates an iterator which calls that closure on each element.
#![allow(unused)] fn main() { fn for_each<F>(self, f: F) where Self: Sized, F: FnMut(Self::Item) }Calls a closure on each element of an iterator.
#![allow(unused)] fn main() { fn filter<P>(self, predicate: P) -> Filter<Self, P> where Self: Sized, P: FnMut(&Self::Item) -> bool }Creates an iterator which uses a closure to determine if an element should be yielded.
#![allow(unused)] fn main() { fn filter_map<B, F>(self, f: F) -> FilterMap<Self, F> where Self: Sized, F: FnMut(Self::Item) -> Option<B> }Creates an iterator that both filters and maps.
#![allow(unused)] fn main() { fn enumerate(self) -> Enumerate<Self> where Self: Sized }Creates an iterator which gives the current iteration count as well as the next value.
#![allow(unused)] fn main() { fn peekable(self) -> Peekable<Self> where Self: Sized }Creates an iterator which can use the peek and peek_mut methods to look at the next element of the iterator without consuming it. See their documentation for more information.
#![allow(unused)] fn main() { fn skip_while<P>(self, predicate: P) -> SkipWhile<Self, P> where Self: Sized, P: FnMut(&Self::Item) -> bool }Creates an iterator that skips elements based on a predicate.
#![allow(unused)] fn main() { fn take_while<P>(self, predicate: P) -> TakeWhile<Self, P> where Self: Sized, P: FnMut(&Self::Item) -> bool }Creates an iterator that yields elements based on a predicate.
#![allow(unused)] fn main() { fn map_while<B, P>(self, predicate: P) -> MapWhile<Self, P> where Self: Sized, P: FnMut(Self::Item) -> Option<B> }Creates an iterator that both yields elements based on a predicate and maps.
#![allow(unused)] fn main() { fn skip(self, n: usize) -> Skip<Self> where Self: Sized }Creates an iterator that skips the first n elements.
#![allow(unused)] fn main() { fn take(self, n: usize) -> Take<Self> where Self: Sized }Creates an iterator that yields the first n elements, or fewer if the underlying iterator ends sooner.
#![allow(unused)] fn main() { fn scan<St, B, F>(self, initial_state: St, f: F) -> Scan<Self, St, F> where Self: Sized, F: FnMut(&mut St, Self::Item) -> Option<B> }An iterator adapter which, like fold, holds internal state, but unlike fold, produces a new iterator.
#![allow(unused)] fn main() { fn flat_map<U, F>(self, f: F) -> FlatMap<Self, U, F> where Self: Sized, U: IntoIterator, F: FnMut(Self::Item) -> U }Creates an iterator that works like map, but flattens nested structure.
#![allow(unused)] fn main() { fn flatten(self) -> Flatten<Self> where Self: Sized, Self::Item: IntoIterator }Creates an iterator that flattens nested structure.
#![allow(unused)] fn main() { fn map_windows<F, R, const N: usize>(self, f: F) -> MapWindows<Self, F, N> where Self: Sized, F: FnMut(&[Self::Item; N]) -> R }🔬This is a nightly-only experimental API. (iter_map_windows)
Calls the given function f for each contiguous window of size N over self and returns an iterator over the outputs of f. Like slice::windows(), the windows during mapping overlap as well.
#![allow(unused)] fn main() { fn fuse(self) -> Fuse<Self> where Self: Sized }Creates an iterator which ends after the first None.
#![allow(unused)] fn main() { fn inspect<F>(self, f: F) -> Inspect<Self, F> where Self: Sized, F: FnMut(&Self::Item) }Does something with each element of an iterator, passing the value on.
#![allow(unused)] fn main() { fn by_ref(&mut self) -> &mut Self where Self: Sized }Borrows an iterator, rather than consuming it.
#![allow(unused)] fn main() { fn collect<B>(self) -> B where B: FromIterator<Self::Item>, Self: Sized }Transforms an iterator into a collection.
#![allow(unused)] fn main() { fn try_collect<B>(&mut self,) -> <<Self::Item as Try>::Residual as Residual<B>>::TryType where Self: Sized, Self::Item: Try, <Self::Item as Try>::Residual: Residual<B>, B: FromIterator<<Self::Item as Try>::Output> }🔬This is a nightly-only experimental API. (iterator_try_collect)
Fallibly transforms an iterator into a collection, short circuiting if a failure is encountered.
#![allow(unused)] fn main() { fn collect_into<E>(self, collection: &mut E) -> &mut E where E: Extend<Self::Item>, Self: Sized }🔬This is a nightly-only experimental API. (iter_collect_into)
Collects all the items from an iterator into a collection.
#![allow(unused)] fn main() { fn partition<B, F>(self, f: F) -> (B, B) where Self: Sized, B: Default + Extend<Self::Item>, F: FnMut(&Self::Item) -> bool }Consumes an iterator, creating two collections from it.
#![allow(unused)] fn main() { fn partition_in_place<'a, T, P>(self, predicate: P) -> usize where T: 'a, Self: Sized + DoubleEndedIterator<Item = &'a mut T>, P: FnMut(&T) -> bool }🔬This is a nightly-only experimental API. (iter_partition_in_place)
Reorders the elements of this iterator in-place according to the given predicate, such that all those that return true precede all those that return false. Returns the number of true elements found.
#![allow(unused)] fn main() { fn is_partitioned<P>(self, predicate: P) -> bool where Self: Sized, P: FnMut(Self::Item) -> bool }🔬This is a nightly-only experimental API. (iter_is_partitioned)
Checks if the elements of this iterator are partitioned according to the given predicate, such that all those that return true precede all those that return false.
#![allow(unused)] fn main() { fn try_fold<B, F, R>(&mut self, init: B, f: F) -> R where Self: Sized, F: FnMut(B, Self::Item) -> R, R: Try<Output = B> }An iterator method that applies a function as long as it returns successfully, producing a single, final value.
#![allow(unused)] fn main() { fn try_for_each<F, R>(&mut self, f: F) -> R where Self: Sized, F: FnMut(Self::Item) -> R, R: Try<Output = ()> }An iterator method that applies a fallible function to each item in the iterator, stopping at the first error and returning that error.
#![allow(unused)] fn main() { fn fold<B, F>(self, init: B, f: F) -> B where Self: Sized, F: FnMut(B, Self::Item) -> B }Folds every element into an accumulator by applying an operation, returning the final result.
#![allow(unused)] fn main() { fn reduce<F>(self, f: F) -> Option<Self::Item> where Self: Sized, F: FnMut(Self::Item, Self::Item) -> Self::Item }Reduces the elements to a single one, by repeatedly applying a reducing operation.
#![allow(unused)] fn main() { fn try_reduce<R>( &mut self, f: impl FnMut(Self::Item, Self::Item) -> R, ) -> <<R as Try>::Residual as Residual<Option<<R as Try>::Output>>>::TryType where Self: Sized, R: Try<Output = Self::Item>, <R as Try>::Residual: Residual<Option<Self::Item>> }🔬This is a nightly-only experimental API. (iterator_try_reduce)
Reduces the elements to a single one by repeatedly applying a reducing operation. If the closure returns a failure, the failure is propagated back to the caller immediately.
#![allow(unused)] fn main() { fn all<F>(&mut self, f: F) -> bool where Self: Sized, F: FnMut(Self::Item) -> bool }Tests if every element of the iterator matches a predicate.
#![allow(unused)] fn main() { fn any<F>(&mut self, f: F) -> bool where Self: Sized, F: FnMut(Self::Item) -> bool }Tests if any element of the iterator matches a predicate.
#![allow(unused)] fn main() { fn find<P>(&mut self, predicate: P) -> Option<Self::Item> where Self: Sized, P: FnMut(&Self::Item) -> bool }Searches for an element of an iterator that satisfies a predicate.
#![allow(unused)] fn main() { fn find_map<B, F>(&mut self, f: F) -> Option<B> where Self: Sized, F: FnMut(Self::Item) -> Option<B> }Applies function to the elements of iterator and returns the first non-none result.
#![allow(unused)] fn main() { fn try_find<R>( &mut self, f: impl FnMut(&Self::Item) -> R, ) -> <<R as Try>::Residual as Residual<Option<Self::Item>>>::TryType where Self: Sized, R: Try<Output = bool>, <R as Try>::Residual: Residual<Option<Self::Item>> }🔬This is a nightly-only experimental API. (try_find)
Applies function to the elements of iterator and returns the first true result or the first error.
#![allow(unused)] fn main() { fn position<P>(&mut self, predicate: P) -> Option<usize> where Self: Sized, P: FnMut(Self::Item) -> bool }Searches for an element in an iterator, returning its index.
#![allow(unused)] fn main() { fn rposition<P>(&mut self, predicate: P) -> Option<usize> where P: FnMut(Self::Item) -> bool, Self: Sized + ExactSizeIterator + DoubleEndedIterator }Searches for an element in an iterator from the right, returning its index.
#![allow(unused)] fn main() { fn max(self) -> Option<Self::Item> where Self: Sized, Self::Item: Ord }Returns the maximum element of an iterator.
#![allow(unused)] fn main() { fn min(self) -> Option<Self::Item> where Self: Sized, Self::Item: Ord }Returns the minimum element of an iterator.
#![allow(unused)] fn main() { fn max_by_key<B, F>(self, f: F) -> Option<Self::Item> where B: Ord, Self: Sized, F: FnMut(&Self::Item) -> B }Returns the element that gives the maximum value from the specified function.
#![allow(unused)] fn main() { fn max_by<F>(self, compare: F) -> Option<Self::Item> where Self: Sized, F: FnMut(&Self::Item, &Self::Item) -> Ordering }Returns the element that gives the maximum value with respect to the specified comparison function.
#![allow(unused)] fn main() { fn min_by_key<B, F>(self, f: F) -> Option<Self::Item> where B: Ord, Self: Sized, F: FnMut(&Self::Item) -> B }Returns the element that gives the minimum value from the specified function.
#![allow(unused)] fn main() { fn min_by<F>(self, compare: F) -> Option<Self::Item> where Self: Sized, F: FnMut(&Self::Item, &Self::Item) -> Ordering }Returns the element that gives the minimum value with respect to the specified comparison function.
#![allow(unused)] fn main() { fn rev(self) -> Rev<Self> where Self: Sized + DoubleEndedIterator }Reverses an iterator’s direction.
#![allow(unused)] fn main() { fn unzip<A, B, FromA, FromB>(self) -> (FromA, FromB) where FromA: Default + Extend<A>, FromB: Default + Extend<B>, Self: Sized + Iterator<Item = (A, B)> }Converts an iterator of pairs into a pair of containers.
#![allow(unused)] fn main() { fn copied<'a, T>(self) -> Copied<Self> where T: 'a + Copy, Self: Sized + Iterator<Item = &'a T> }Creates an iterator which copies all of its elements.
#![allow(unused)] fn main() { fn cloned<'a, T>(self) -> Cloned<Self> where T: 'a + Clone, Self: Sized + Iterator<Item = &'a T> }Creates an iterator which clones all of its elements.
#![allow(unused)] fn main() { fn array_chunks<const N: usize>(self) -> ArrayChunks<Self, N> where Self: Sized }🔬This is a nightly-only experimental API. (iter_array_chunks)
Returns an iterator over N elements of the iterator at a time.
#![allow(unused)] fn main() { fn sum<S>(self) -> S where Self: Sized, S: Sum<Self::Item> }Sums the elements of an iterator.
#![allow(unused)] fn main() { fn product<P>(self) -> P where Self: Sized, P: Product<Self::Item> }Iterates over the entire iterator, multiplying all the elements
#![allow(unused)] fn main() { fn cmp<I>(self, other: I) -> Ordering where I: IntoIterator<Item = Self::Item>, Self::Item: Ord, Self: Sized }Lexicographically compares the elements of this Iterator with those of another.
#![allow(unused)] fn main() { fn cmp_by<I, F>(self, other: I, cmp: F) -> Ordering where Self: Sized, I: IntoIterator, F: FnMut(Self::Item, <I as IntoIterator>::Item) -> Ordering }🔬This is a nightly-only experimental API. (iter_order_by)
Lexicographically compares the elements of this Iterator with those of another with respect to the specified comparison function.
#![allow(unused)] fn main() { fn partial_cmp<I>(self, other: I) -> Option<Ordering> where I: IntoIterator, Self::Item: PartialOrd<<I as IntoIterator>::Item>, Self: Sized }Lexicographically compares the PartialOrd elements of this Iterator with those of another. The comparison works like short-circuit evaluation, returning a result without comparing the remaining elements. As soon as an order can be determined, the evaluation stops and a result is returned.
#![allow(unused)] fn main() { fn partial_cmp_by<I, F>(self, other: I, partial_cmp: F) -> Option<Ordering> where Self: Sized, I: IntoIterator, F: FnMut(Self::Item, <I as IntoIterator>::Item) -> Option<Ordering> }🔬This is a nightly-only experimental API. (iter_order_by)
Lexicographically compares the elements of this Iterator with those of another with respect to the specified comparison function.
#![allow(unused)] fn main() { fn eq<I>(self, other: I) -> bool where I: IntoIterator, Self::Item: PartialEq<<I as IntoIterator>::Item>, Self: Sized }Determines if the elements of this Iterator are equal to those of another.
#![allow(unused)] fn main() { fn eq_by<I, F>(self, other: I, eq: F) -> bool where Self: Sized, I: IntoIterator, F: FnMut(Self::Item, <I as IntoIterator>::Item) -> bool }🔬This is a nightly-only experimental API. (iter_order_by)
Determines if the elements of this Iterator are equal to those of another with respect to the specified equality function.
#![allow(unused)] fn main() { fn ne<I>(self, other: I) -> bool where I: IntoIterator, Self::Item: PartialEq<<I as IntoIterator>::Item>, Self: Sized }Determines if the elements of this Iterator are not equal to those of another.
#![allow(unused)] fn main() { fn lt<I>(self, other: I) -> bool where I: IntoIterator, Self::Item: PartialOrd<<I as IntoIterator>::Item>, Self: Sized }Determines if the elements of this Iterator are lexicographically less than those of another.
#![allow(unused)] fn main() { fn le<I>(self, other: I) -> bool where I: IntoIterator, Self::Item: PartialOrd<<I as IntoIterator>::Item>, Self: Sized }Determines if the elements of this Iterator are lexicographically less or equal to those of another.
#![allow(unused)] fn main() { fn gt<I>(self, other: I) -> bool where I: IntoIterator, Self::Item: PartialOrd<<I as IntoIterator>::Item>, Self: Sized }Determines if the elements of this Iterator are lexicographically greater than those of another.
#![allow(unused)] fn main() { fn ge<I>(self, other: I) -> bool where I: IntoIterator, Self::Item: PartialOrd<<I as IntoIterator>::Item>, Self: Sized }Determines if the elements of this Iterator are lexicographically greater than or equal to those of another.
#![allow(unused)] fn main() { fn is_sorted(self) -> bool where Self: Sized, Self::Item: PartialOrd }Checks if the elements of this iterator are sorted.
#![allow(unused)] fn main() { fn is_sorted_by<F>(self, compare: F) -> bool where Self: Sized, F: FnMut(&Self::Item, &Self::Item) -> bool }Checks if the elements of this iterator are sorted using the given comparator function.
#![allow(unused)] fn main() { fn is_sorted_by_key<F, K>(self, f: F) -> bool where Self: Sized, F: FnMut(Self::Item) -> K, K: PartialOrd }Checks if the elements of this iterator are sorted using the given key extraction function.
impl FusedIterator for Iter
Field
#![allow(unused)] fn main() { pub struct Field { ... } }
Defines a region of bits.
Each field has a unit, size, and a flag indicating if the field represents a signed integer. The unit is the minimum number of bits extracted by the field. The size is the total number of units extracted by the field.
The flag comes into play when a field extracts more bits than are available. If at least one bit is still available and the flag is true, the bit string is sign extended to the length of the field.
Constants
Common field types.
| Constant | Unit | Size | Signed |
|---|---|---|---|
Field::BIT | 1 | 1 | |
Field::BYTES | 8 | UNBOUND | |
Field::SIGNED | 1 | UNBOUND | X |
Field::SIGNED_2 | 1 | 2 | X |
Field::SIGNED_4 | 1 | 4 | X |
Field::SIGNED_8 | 1 | 8 | X |
Field::SIGNED_16 | 1 | 16 | X |
Field::SIGNED_32 | 1 | 32 | X |
Field::SIGNED_64 | 1 | 64 | X |
Field::UNSIGNED | 1 | UNBOUND | |
Field::UNSIGNED_2 | 1 | 2 | |
Field::UNSIGNED_4 | 1 | 4 | |
Field::UNSIGNED_8 | 1 | 8 | |
Field::UNSIGNED_16 | 1 | 16 | |
Field::UNSIGNED_32 | 1 | 32 | |
Field::UNSIGNED_64 | 1 | 64 |
Implementation
#![allow(unused)] fn main() { pub fn aligned(unit: usize, size: isize, signed: bool) -> Self }Bit field aligned to a given unit
When m and n are negative Bits::aligned(m, signed) == Bits::aligned(n, signed), even if m != n.
#![allow(unused)] fn main() { let field = Field::aligned(7, 48, false); assert_eq!(field.unit(), 7); assert_eq!(field.size(), 48); assert_eq!(field.len(), 48 * 7); let mfield = Field::aligned(7, -1, true); let nfield = Field::aligned(7, isize::MIN, true); assert_eq!(mfield, nfield); assert_eq!(mfield.size(), -1); assert_eq!(nfield.size(), -1); assert!(mfield.is_signed()); }
#![allow(unused)] fn main() { pub fn bytes(size: isize, signed: bool) -> Self }Byte-aligned bit field
When m and n are negative Bits::bytes(m, signed) == Bits::bytes(n, signed), even if m != n.
#![allow(unused)] fn main() { let field = Field::bytes(48, false); assert_eq!(field.unit(), 8); assert_eq!(field.size(), 48); assert_eq!(field.len(), 48 * 8); let mfield = Field::bytes(-1, true); let nfield = Field::bytes(isize::MIN, true); assert_eq!(mfield, nfield); assert_eq!(mfield.size(), -1); assert_eq!(nfield.size(), -1); assert!(mfield.is_signed()); }
#![allow(unused)] fn main() { pub fn signed(size: isize) -> Self }Bit field
When m and n are negative Bits::bits(m, signed) == Bits::bits(n, signed), even if m != n.
#![allow(unused)] fn main() { let field = Field::signed(96); assert_eq!(field.unit(), 1); assert_eq!(field.size(), 96); assert_eq!(field.len(), 96); assert!(field.is_signed()); let mfield = Field::signed(-1); let nfield = Field::signed(isize::MIN); assert_eq!(mfield, nfield); assert_eq!(mfield.size(), -1); assert_eq!(nfield.size(), -1); assert!(mfield.is_signed()); }
#![allow(unused)] fn main() { pub fn unsigned(size: isize) -> Self }Bit field
When m and n are negative Bits::bits(m, signed) == Bits::bits(n, signed), even if m != n.
#![allow(unused)] fn main() { let field = Field::unsigned(96); assert_eq!(field.unit(), 1); assert_eq!(field.size(), 96); assert_eq!(field.len(), 96); assert!(!field.is_signed()); let mfield = Field::unsigned(-1); let nfield = Field::unsigned(isize::MIN); assert_eq!(mfield, nfield); assert_eq!(mfield.size(), -1); assert_eq!(nfield.size(), -1); assert!(!mfield.is_signed()); }
#![allow(unused)] fn main() { pub fn is_signed(&self) -> bool }Whether this field represents a signed integer.
#![allow(unused)] fn main() { assert!(Field::aligned(7, 48, true).is_signed()); assert!(!Field::aligned(7, 48, false).is_signed()); assert!(Field::bytes(-1, true).is_signed()); assert!(!Field::bytes(-1, false).is_signed()); assert!(Field::signed(96).is_signed()); assert!(!Field::unsigned(96).is_signed()); }
#![allow(unused)] fn main() { pub fn len(&self) -> isize }Number of bits extracted by the field
-1 means that the field extracts the maximum number of bits that can be divided by the unit of the field. Extraction never exceeds the remaining the number of bits.
#![allow(unused)] fn main() { assert_eq!(Field::aligned(0, -1, true).len(), 0); assert_eq!(Field::aligned(7, 0, false).len(), 0); assert_eq!(Field::aligned(7, 48, true).len(), 48 * 7); assert_eq!(Field::aligned(7, -1, false).len(), -1); assert_eq!(Field::aligned(7, isize::MIN, true).len(), -1); assert_eq!(Field::bytes(48, true).len(), 48 * 8); assert_eq!(Field::bytes(-1, false).len(), -1); assert_eq!(Field::bytes(isize::MIN, true).len(), -1); assert_eq!(Field::signed(48).len(), 48); assert_eq!(Field::signed(-1).len(), -1); assert_eq!(Field::signed(isize::MIN).len(), -1); assert_eq!(Field::unsigned(48).len(), 48); assert_eq!(Field::unsigned(-1).len(), -1); assert_eq!(Field::unsigned(isize::MIN).len(), -1); }
#![allow(unused)] fn main() { pub fn size(&self) -> isize }Number of units in the field
#![allow(unused)] fn main() { assert_eq!(Field::aligned(7, 48, true).size(), 48); assert_eq!(Field::aligned(7, -1, false).size(), -1); assert_eq!(Field::aligned(7, isize::MIN, true).size(), -1); assert_eq!(Field::bytes(48, true).size(), 48); assert_eq!(Field::bytes(-1, false).size(), -1); assert_eq!(Field::bytes(isize::MIN, true).size(), -1); assert_eq!(Field::signed(48).size(), 48); assert_eq!(Field::signed(-1).size(), -1); assert_eq!(Field::signed(isize::MIN).size(), -1); assert_eq!(Field::unsigned(48).size(), 48); assert_eq!(Field::unsigned(-1).size(), -1); assert_eq!(Field::unsigned(isize::MIN).size(), -1); }
#![allow(unused)] fn main() { pub fn unit(&self) -> usize }Minimum number of bits extracted by the field.
#![allow(unused)] fn main() { assert_eq!(Field::aligned(7, 48, true).unit(), 7); assert_eq!(Field::aligned(7, -1, false).unit(), 7); assert_eq!(Field::aligned(7, isize::MIN, true).unit(), 7); assert_eq!(Field::bytes(48, true).unit(), 8); assert_eq!(Field::bytes(-1, false).unit(), 8); assert_eq!(Field::bytes(isize::MIN, true).unit(), 8); assert_eq!(Field::signed(48).unit(), 1); assert_eq!(Field::signed(-1).unit(), 1); assert_eq!(Field::signed(isize::MIN).unit(), 1); assert_eq!(Field::unsigned(48).unit(), 1); assert_eq!(Field::unsigned(-1).unit(), 1); assert_eq!(Field::unsigned(isize::MIN).unit(), 1); }
Traits
Clone
#![allow(unused)] fn main() { fn clone(&self) -> Field }Returns a copy of the value.
#![allow(unused)] fn main() { fn clone_from(&mut self, source: &Self) }Performs copy-assignment from source.
Debug
#![allow(unused)] fn main() { fn fmt(&self, f: &mut Formatter<'_>) -> Result }Formats the value using the given formatter. Read more
PartialEq
#![allow(unused)] fn main() { fn eq(&self, other: &Field) -> bool }Tests for self and other values to be equal, and is used by ==.
#![allow(unused)] fn main() { fn ne(&self, other: &Rhs) -> bool }Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Copy
Eq
Scheme
#![allow(unused)] fn main() { pub struct Scheme(...); }
Defines a pattern giving meaning to different regions of a bit string.
Implementation
#![allow(unused)] fn main() { pub fn new<I: IntoIterator<Item = Field>>(pattern: I) -> Self }Creates a new scheme from the sequence of fields.
#![allow(unused)] fn main() { let unsigned_3 = Field::unsigned(3); let scheme = Scheme::new([ Field::signed(5), Field::BIT, unsigned_3, unsigned_3, Field::UNSIGNED_4, Field::aligned(23, 1, true), Field::unsigned(32), Field::signed(32), Field::bytes(4, false), Field::aligned(6, 8, true) ]); let time_since_epoch = Field::UNSIGNED_64; let identifier = Field::UNSIGNED_8; let datatype = Field::UNSIGNED_4; let data = Field::BYTES; let scheme2 = Scheme::new([time_since_epoch, identifier, datatype, data]); }
#![allow(unused)] fn main() { pub fn len(&self) -> isize }Number of bits extracted
If any field in the scheme has a negative length, this method returns -1, indicating the scheme extracts a variable number of bits.
#![allow(unused)] fn main() { let unsigned_3 = Field::unsigned(3); let scheme = Scheme::new([ Field::signed(5), Field::BIT, unsigned_3, unsigned_3, Field::UNSIGNED_4 ]); assert_eq!(scheme.len(), 16); let scheme2 = Scheme::new([ Field::aligned(23, 1, true), Field::unsigned(0), Field::unsigned(32), Field::bytes(4, false), Field::aligned(0, 8, false), Field::aligned(7, 8, true) ]); let scheme3 = Scheme::new([ Field::aligned(23, 1, true), Field::unsigned(0), Field::unsigned(32), Field::signed(-1), Field::bytes(4, false), Field::aligned(0, 8, false), Field::aligned(7, 8, true) ]); assert_eq!(scheme3.len(), -1); }#![allow(unused)] fn main() { pub fn split_bits(&self, bits: &Bits) -> Vec<Bits> }Extracts the data associated with the fields of this scheme
#![allow(unused)] fn main() { let unsigned_3 = Field::unsigned(3); let scheme = Scheme::new([ Field::signed(5), Field::BIT, unsigned_3, unsigned_3, Field::UNSIGNED_4 ]); let bits = Bits::new([0xBA, 0x1C]); let parts = scheme.split_bits(&bits); assert_eq!(bits_as::int8(&parts[0]), -6); assert_eq!(bits_as::uint8(&parts[1]), 1); assert_eq!(bits_as::uint8(&parts[2]), 2); assert_eq!(bits_as::uint8(&parts[3]), 6); assert_eq!(bits_as::uint8(&parts[4]), 1); let bits2 = Bits::new([0xBA]); let parts2 = scheme.split_bits(&bits2); assert_eq!(bits_as::int8(&parts2[0]), -6); assert_eq!(bits_as::uint8(&parts2[1]), 1); assert_eq!(bits_as::uint8(&parts2[2]), 2); assert_eq!(bits_as::uint8(&parts2[3]), 0); assert_eq!(bits_as::uint8(&parts2[4]), 0); let scheme2 = Scheme::new([ Field::aligned(23, -1, true), Field::aligned(0, -1, false), Field::bytes(-1, false), Field::signed(-1), Field::unsigned(-1), Field::unsigned(0), Field::unsigned(32), Field::bytes(4, false), Field::aligned(0, 8, false), Field::aligned(7, 8, true), Field::signed(-1), Field::unsigned(-1) ]); let bytes = [0xBA, 0xDC, 0xFE, 0x10, 0x32]; let bits3 = Bits::from(bytes.as_slice()); // 38-bits let parts = scheme2.split_bits(&bits3); assert_eq!(bits_as::int32(&parts[0]), 0xFFFEDCBAu32 as i32); assert_eq!(parts[1], Bits::empty()); assert_eq!(bits_as::vec_u8(&parts[2]), vec![0x21]); assert_eq!(bits_as::vec_i32(&parts[3]), vec![-28]); assert_eq!(parts[4], Bits::empty()); assert_eq!(parts[5], Bits::empty()); assert_eq!(parts[6], Bits::zeros(32)); assert_eq!(parts[7], Bits::zeros(32)); assert_eq!(parts[8], Bits::empty()); assert_eq!(parts[9], Bits::zeros(56)); assert_eq!(parts[10], Bits::empty()); assert_eq!(parts[11], Bits::empty()); }
#![allow(unused)] fn main() { pub fn split_bits_at(&self, index: usize, bits: &Bits) -> Vec<Bits> }Extracts the data associated with the fields of this scheme.
#![allow(unused)] fn main() { let unsigned_3 = Field::unsigned(3); let scheme = Scheme::new([ Field::signed(5), Field::BIT, unsigned_3, unsigned_3, Field::UNSIGNED_4 ]); let bits = Bits::new([0xBA, 0x1C]); let parts = scheme.split_bits_at(0, &bits); assert_eq!(bits_as::int8(&parts[0]), -6); assert_eq!(bits_as::uint8(&parts[1]), 1); assert_eq!(bits_as::uint8(&parts[2]), 2); assert_eq!(bits_as::uint8(&parts[3]), 6); assert_eq!(bits_as::uint8(&parts[4]), 1); let parts2 = scheme.split_bits_at(8, &bits); assert_eq!(bits_as::int8(&parts2[0]), -4); assert_eq!(bits_as::uint8(&parts2[1]), 0); assert_eq!(bits_as::uint8(&parts2[2]), 0); assert_eq!(bits_as::uint8(&parts2[3]), 0); assert_eq!(bits_as::uint8(&parts2[4]), 0); let scheme2 = Scheme::new([ Field::aligned(23, -1, true), Field::aligned(0, -1, false), Field::bytes(-1, false), Field::signed(-1), Field::unsigned(-1), Field::unsigned(0), Field::unsigned(32), Field::bytes(4, false), Field::aligned(0, 8, false), Field::aligned(7, 8, true), Field::signed(-1), Field::unsigned(-1) ]); let bytes = [0xBA, 0xDC, 0xFE, 0x10, 0x32]; let bits3 = Bits::from(bytes.as_slice()); // 38-bits let parts = scheme2.split_bits_at(4, &bits3); assert_eq!(bits_as::int32(&parts[0]), 0x0FEDCB); assert_eq!(parts[1], Bits::empty()); assert_eq!(bits_as::vec_u8(&parts[2]), vec![0x42]); assert_eq!(bits_as::vec_i32(&parts[3]), vec![-2]); assert_eq!(parts[4], Bits::empty()); assert_eq!(parts[5], Bits::empty()); assert_eq!(parts[6], Bits::zeros(32)); assert_eq!(parts[7], Bits::zeros(32)); assert_eq!(parts[8], Bits::empty()); assert_eq!(parts[9], Bits::zeros(56)); assert_eq!(parts[10], Bits::empty()); assert_eq!(parts[11], Bits::empty());f }
#![allow(unused)] fn main() { pub fn split_bytes<I: IntoIterator<Item = u8>>(&self, bytes: I) -> Vec<Bits> }Extracts the data associated with the fields of this scheme
#![allow(unused)] fn main() { let unsigned_3 = Field::unsigned(3); let scheme = Scheme::new([ Field::signed(5), Field::BIT, unsigned_3, unsigned_3, Field::UNSIGNED_4 ]); let parts = scheme.split_bytes([0xBA, 0x1C]); assert_eq!(bits_as::int8(&parts[0]), -6); assert_eq!(bits_as::uint8(&parts[1]), 1); assert_eq!(bits_as::uint8(&parts[2]), 2); assert_eq!(bits_as::uint8(&parts[3]), 6); assert_eq!(bits_as::uint8(&parts[4]), 1); let parts2 = scheme.split_bytes([0xBA]); assert_eq!(bits_as::int8(&parts2[0]), -6); assert_eq!(bits_as::uint8(&parts2[1]), 1); assert_eq!(bits_as::uint8(&parts2[2]), 2); assert_eq!(bits_as::uint8(&parts2[3]), 0); assert_eq!(bits_as::uint8(&parts2[4]), 0); let scheme2 = Scheme::new([ Field::aligned(23, -1, true), Field::aligned(0, -1, false), Field::bytes(-1, false), Field::signed(-1), Field::unsigned(-1), Field::unsigned(0), Field::unsigned(32), Field::bytes(4, false), Field::aligned(0, 8, false), Field::aligned(7, 8, true), Field::signed(-1), Field::unsigned(-1) ]); let parts = scheme2.split_bytes(vec![0xBA, 0xDC, 0xFE, 0x10, 0x32]); assert_eq!(bits_as::int32(&parts[0]), 0xFFFEDCBAu32 as i32); assert_eq!(parts[1], Bits::empty()); assert_eq!(bits_as::vec_u8(&parts[2]), vec![0x21, 0x64]); assert_eq!(parts[3], Bits::zeros(1)); assert_eq!(bits_as::int64(&parts[4]), 0); assert_eq!(parts[5], Bits::empty()); assert_eq!(parts[6], Bits::zeros(32)); assert_eq!(parts[7], Bits::zeros(32)); assert_eq!(parts[8], Bits::empty()); assert_eq!(parts[9], Bits::zeros(56)); assert_eq!(parts[10], Bits::empty()); assert_eq!(parts[11], Bits::empty()); }
#![allow(unused)] fn main() { pub fn split_bytes_at<I: IntoIterator<Item = u8>>( &self, index: usize, bytes: I, ) -> Vec<Bits> }Extracts the data associated with the fields of this scheme.
#![allow(unused)] fn main() { let unsigned_3 = Field::unsigned(3); let scheme = Scheme::new([ Field::signed(5), Field::BIT, unsigned_3, unsigned_3, Field::UNSIGNED_4 ]); let parts = scheme.split_bytes_at(0, [0xBA, 0x1C]); assert_eq!(bits_as::int8(&parts[0]), -6); assert_eq!(bits_as::uint8(&parts[1]), 1); assert_eq!(bits_as::uint8(&parts[2]), 2); assert_eq!(bits_as::uint8(&parts[3]), 6); assert_eq!(bits_as::uint8(&parts[4]), 1); let parts2 = scheme.split_bytes_at(8, [0xBA, 0x1C]); assert_eq!(bits_as::int8(&parts2[0]), -4); assert_eq!(bits_as::uint8(&parts2[1]), 0); assert_eq!(bits_as::uint8(&parts2[2]), 0); assert_eq!(bits_as::uint8(&parts2[3]), 0); assert_eq!(bits_as::uint8(&parts2[4]), 0); let scheme2 = Scheme::new([ Field::aligned(23, -1, true), Field::aligned(0, -1, false), Field::bytes(-1, false), Field::signed(-1), Field::unsigned(-1), Field::unsigned(0), Field::unsigned(32), Field::bytes(4, false), Field::aligned(0, 8, false), Field::aligned(7, 8, true), Field::signed(-1), Field::unsigned(-1) ]); let parts = scheme2.split_bytes_at(4, [0xBA, 0xDC, 0xFE, 0x10, 0x32]); assert_eq!(bits_as::int32(&parts[0]), 0x0FEDCB); assert_eq!(parts[1], Bits::empty()); assert_eq!(bits_as::vec_u8(&parts[2]), vec![0x42]); assert_eq!(bits_as::vec_i32(&parts[3]), vec![6]); assert_eq!(parts[4], Bits::empty()); assert_eq!(parts[5], Bits::empty()); assert_eq!(parts[6], Bits::zeros(32)); assert_eq!(parts[7], Bits::zeros(32)); assert_eq!(parts[8], Bits::empty()); assert_eq!(parts[9], Bits::zeros(56)); assert_eq!(parts[10], Bits::empty()); assert_eq!(parts[11], Bits::empty()); }
contexts
Reference
ContextMap
#![allow(unused)] fn main() { pub struct ContextManager<K, V, S = RandomState> { ... } }
Implementations
ContextManager<K, V, S>
K: Hash + Eq + Clone, V: Clone, S: BuildHasher + Clone
#![allow(unused)] fn main() { pub fn fork(&self) -> Option<ContextManager<K, V, S>> }Creates a new context manager initialized with a clone of the current local context.
Equivalent to
manager.fork_from(0).#![allow(unused)] fn main() { let mut manager = ContextManager::with_capacity(3); assert_eq!(manager.fork(), None); manager.push(HashMap::from([("w", 1)])); manager.push(HashMap::from([("x", 2)])); manager.push(HashMap::from([("y", 3)])); let forked = manager.fork().unwrap(); assert_eq!(forked.get("w"), None); assert_eq!(forked.get("x"), None); assert_eq!(&forked["y"], &3); }
#![allow(unused)] fn main() { pub fn fork_from(&self, index: usize) -> Option<ContextManager<K, V, S>> }Creates a new context manager initialized with clones of all contexts from the local one up to and including the one at index.
manager.fork_from(manager.len() - 1)is equivalent tomanager.clone().#![allow(unused)] fn main() { let mut manager = ContextManager::with_capacity(3); manager.push(HashMap::from([("w", 1)])); manager.push(HashMap::from([("x", 2)])); manager.push(HashMap::from([("y", 3)])); let forked = manager.fork_from(1).unwrap(); assert_eq!(forked.get("w"), None); assert_eq!(&forked["x"], &2); assert_eq!(&forked["y"], &3); let invalid_fork = manager.fork_from(3); assert_eq!(invalid_fork, None); }
#![allow(unused)] fn main() { pub fn push_local(&mut self) }Adds a new context that is a clone of the local context, if one is present.
#![allow(unused)] fn main() { let mut manager = ContextManager::from([("w", 1)]); manager.push_local(); assert_eq!(&manager["w"], &1); let w = manager.get_mut("w").unwrap(); *w = 2; assert_eq!(&manager["w"], &2); manager.pop(); assert_eq!(&manager["w"], &1); }
#![allow(unused)] fn main() { pub fn push_with_local(&mut self, context: HashMap<K, V, S>) }Adds a new local context merged with the previous local context.
The new context has higher precedence.
#![allow(unused)] fn main() { let mut manager = ContextManager::from([("w", 1)]); let context = HashMap::from([("x", 2)]); manager.push_with_local(context); assert_eq!(manager.get_local("w"), Some(&1)); assert_eq!(&manager["x"], &2); manager.pop(); assert_eq!(&manager["w"], &1); assert_eq!(manager.get("x"), None); }
K: Ord
#![allow(unused)] fn main() { pub fn collapse_ordered(self) -> BTreeMap<K, V> }Aggregates all contexts into a single map where keys have their most recent value.
#![allow(unused)] fn main() { let mut manager = ContextManager::with_capacity(3); manager.push(HashMap::from([("y", 3)])); manager.push(HashMap::from([("w", 1), ("x", 2)])); manager.push(HashMap::from([("y", 4), ("z", 3)])); let map = manager.collapse_ordered(); assert_eq!(&map["w"], &1); assert_eq!(&map["x"], &2); assert_eq!(&map["y"], &4); assert_eq!(&map["z"], &3); }
#![allow(unused)] fn main() { pub fn collapse_into_ordered(self, src: &mut BTreeMap<K, V>) }Aggregates all contexts storing each key and its most recent value into src.
#![allow(unused)] fn main() { let mut manager = ContextManager::with_capacity(3); manager.push(HashMap::from([("y", 4)])); manager.push(HashMap::from([("w", 2), ("x", 3)])); let mut map = BTreeMap::from([("v", 1), ("x", 2), ("z", 5)]); manager.collapse_into_ordered(&mut map); assert_eq!(&map["v"], &1); assert_eq!(&map["w"], &2); assert_eq!(&map["x"], &3); assert_eq!(&map["y"], &4); assert_eq!(&map["z"], &5); }
ContextManager<K, V, RandomState>
K: Hash + Eq
#![allow(unused)] fn main() { pub fn with_empty() -> Self }Creates a context manager initialized with an empty context.
#![allow(unused)] fn main() { let mut manager = ContextManager::with_empty(); manager.insert("x", 1); assert_eq!(&manager["x"], &1); }
#![allow(unused)] fn main() { pub fn new() -> Self }Creates an empty context manager.
#![allow(unused)] fn main() { let mut manager = ContextManager::new(); manager.insert("x", 1); assert!(!manager.contains_key("x")); }
#![allow(unused)] fn main() { pub fn with_capacity(capacity: usize) -> Self }Creates an empty context manager with space for capacity contexts.
#![allow(unused)] fn main() { let mut manager = ContextManager::with_capacity(3); manager.insert("x", 1); assert!(!manager.contains_key("x")); }
#![allow(unused)] fn main() { pub fn collapse(self) -> HashMap<K, V> }Aggregates all contexts into a single map where keys have their most recent value.
#![allow(unused)] fn main() { let mut manager = ContextManager::with_capacity(3); manager.push(HashMap::from([("y", 3)])); manager.push(HashMap::from([("w", 1), ("x", 2)])); manager.push(HashMap::from([("y", 4), ("z", 3)])); let map = manager.collapse(); assert_eq!(&map["w"], &1); assert_eq!(&map["x"], &2); assert_eq!(&map["y"], &4); assert_eq!(&map["z"], &3); }
#![allow(unused)] fn main() { pub fn push_empty(&mut self) }Adds an empty local context.
#![allow(unused)] fn main() { let mut manager = ContextManager::with_empty(); manager.insert("x", 1); assert_eq!(manager.get_local("x"), Some(&1)); manager.push_empty(); assert_eq!(manager.get_local("x"), None); }
#![allow(unused)] fn main() { pub fn collapse_into(self, src: &mut HashMap<K, V, S>) }Aggregates all contexts storing each key and its most recent value into src.
#![allow(unused)] fn main() { let mut manager = ContextManager::with_capacity(3); manager.push(HashMap::from([("y", 4)])); manager.push(HashMap::from([("w", 2), ("x", 3)])); let mut map = HashMap::from([("v", 1), ("x", 2), ("z", 5)]); manager.collapse_into(&mut map); assert_eq!(&map["v"], &1); assert_eq!(&map["w"], &2); assert_eq!(&map["x"], &3); assert_eq!(&map["y"], &4); assert_eq!(&map["z"], &5); }
#![allow(unused)] fn main() { pub fn contains_key<Q>(&self, key: &Q) -> bool where K: Borrow<Q>, Q: ?Sized + Hash + Eq }Whether a key is present in the context.
#![allow(unused)] fn main() { let mut manager = ContextManager::with_capacity(3); manager.push(HashMap::from([("w", 1)])); manager.push(HashMap::from([("x", 2)])); assert!(manager.contains_key(&"w")); assert!(manager.contains_key(&"x")); assert!(!manager.contains_key(&"y")); }
#![allow(unused)] fn main() { pub fn contains_local_key<Q>(&self, key: &Q) -> bool where K: Borrow<Q>, Q: ?Sized + Hash + Eq }Whether a key is present in the local context
#![allow(unused)] fn main() { let mut manager = ContextManager::new(); manager.push(HashMap::from([("w", 1)])); assert!(manager.contains_local_key("w")); manager.push_empty(); assert!(!manager.contains_local_key("w")); }
#![allow(unused)] fn main() { pub fn get<Q>(&self, key: &Q) -> Option<&V> where K: Borrow<Q>, Q: ?Sized + Hash + Eq }Returns a reference to the value associated with key.
#![allow(unused)] fn main() { let mut manager = ContextManager::with_capacity(3); manager.push(HashMap::from([("w", 1)])); manager.push(HashMap::from([("x", 2)])); assert_eq!(manager.get(&"w"), Some(&1)); assert_eq!(manager.get(&"x"), Some(&2)); assert_eq!(manager.get(&"y"), None); }
#![allow(unused)] fn main() { pub fn get_all<Q>(&self, key: &Q) -> Vec<&V> where K: Borrow<Q>, Q: ?Sized + Hash + Eq }Returns a vector of references to all values associated with key, ordered by precedence.
#![allow(unused)] fn main() { let mut manager = ContextManager::with_capacity(3); manager.push(HashMap::from([("w", 1)])); manager.push(HashMap::from([("w", 2)])); manager.push(HashMap::from([("w", 3)])); assert_eq!(manager.get_all(&"w"), vec![&3, &2, &1]); assert_eq!(manager.get_all(&"x"), Vec::<&i32>::new()); }
#![allow(unused)] fn main() { pub fn get_from<Q>(&self, index: usize, key: &Q) -> Option<&V> where K: Borrow<Q>, Q: ?Sized + Hash + Eq }Returns a reference to the value associated with key starting with the context at index.
#![allow(unused)] fn main() { let mut manager = ContextManager::with_capacity(3); manager.push(HashMap::from([("w", 1)])); manager.push(HashMap::from([("x", 2)])); manager.push(HashMap::from([("w", 3)])); assert_eq!(manager.get_from(0, &"w"), Some(&3)); assert_eq!(manager.get_from(1, &"w"), Some(&1)); assert_eq!(manager.get_from(2, &"w"), Some(&1)); assert_eq!(manager.get_from(3, &"w"), None); }
#![allow(unused)] fn main() { pub fn get_local<Q>(&self, key: &Q) -> Option<&V> where K: Borrow<Q>, Q: ?Sized + Hash + Eq }Returns a reference to the value associated with key in the local context.
#![allow(unused)] fn main() { let mut manager = ContextManager::from([("w", 1)]); assert_eq!(manager.get_local(&"w"), Some(&1)); manager.push_empty(); assert_eq!(manager.get_local(&"w"), None); }
#![allow(unused)] fn main() { pub fn get_local_mut<Q>(&mut self, key: &Q) -> Option<&mut V> where K: Borrow<Q>, Q: ?Sized + Hash + Eq }Returns a mutable reference to the value associated with key in the local context.
#![allow(unused)] fn main() { let mut manager = ContextManager::from([("w", 1)]); if let Some(w) = manager.get_local_mut("w") { *w = 2; } assert_eq!(manager.get(&"w"), Some(&2)); manager.push_empty(); assert_eq!(manager.get_local_mut(&"w"), None); }
#![allow(unused)] fn main() { pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V> where K: Borrow<Q>, Q: ?Sized + Hash + Eq }Returns a mutable reference to the value associated with key.
#![allow(unused)] fn main() { let mut manager = ContextManager::from([("w", 1)]); if let Some(v) = manager.get_mut("w") { *v = 2; } assert_eq!(&manager["w"], &2); assert_eq!(manager.get_mut(&"x"), None); }
#![allow(unused)] fn main() { pub fn get_mut_from<Q>(&mut self, index: usize, key: &Q) -> Option<&mut V> where K: Borrow<Q>, Q: ?Sized + Hash + Eq }Returns a reference to the value associated with key starting with the context at index.
#![allow(unused)] fn main() { let mut manager = ContextManager::new(); manager.push(HashMap::from([("w", 1)])); manager.push(HashMap::from([("w", 2)])); if let Some(v) = manager.get_mut_from(1, "w") { *v = 3; } assert_eq!(manager.get_from(0, "w"), Some(&2)); assert_eq!(manager.get_from(1, "w"), Some(&3)); assert_eq!(manager.get_mut_from(2, "w"), None); }
#![allow(unused)] fn main() { pub fn insert(&mut self, key: K, value: V) -> Option<V> }Associates value with key in the local context if there is one.
#![allow(unused)] fn main() { let mut manager = ContextManager::new(); assert_eq!(manager.insert("w", 1), None); assert_eq!(manager.get("w"), None); manager.push_empty(); manager.insert("w", 1); assert_eq!(manager.insert("w", 2), Some(1)); manager.push_empty(); assert_eq!(manager.insert("w", 3), None); }
#![allow(unused)] fn main() { pub fn remove<Q>(&mut self, key: &Q) -> Option<V> where K: Borrow<Q>, Q: ?Sized + Hash + Eq }Removes key from the local context if one is present.
#![allow(unused)] fn main() { let mut manager = ContextManager::with_capacity(3); assert_eq!(manager.remove("w"), None); manager.push(HashMap::from([("w", 1)])); manager.push(HashMap::from([("w", 2)])); assert_eq!(manager.remove("w"), Some(2)); assert_eq!(manager.remove("w"), None); assert_eq!(&manager["w"], &1); }
#![allow(unused)] fn main() { pub fn remove_all<Q>(&mut self, key: &Q) -> Vec<V> where K: Borrow<Q>, Q: ?Sized + Hash + Eq }Removes all instances of key from the context manager, returning a vector of the values, ordered by precedence.
#![allow(unused)] fn main() { let mut manager = ContextManager::with_capacity(3); manager.push(HashMap::from([("w", 1)])); manager.push(HashMap::from([("w", 2)])); manager.push(HashMap::from([("w", 3)])); assert_eq!(manager.remove_all(&"w"), vec![3, 2, 1]); assert_eq!(manager.remove_all(&"x"), vec![]); }
Traits
Clone
K: Clone, V: Clone, S: Clone
#![allow(unused)] fn main() { fn clone(&self) -> ContextManager<K, V, S> }Returns a duplicate of the value. Read more
#![allow(unused)] fn main() { const fn clone_from(&mut self, source: &ContextManager<K, V, S>) }Performs copy-assignment from source. Read more
Debug
K: Debug, V: Debug, S: Debug
#![allow(unused)] fn main() { fn fmt(&self, f: &mut Formatter<'_>) -> Result }Formats the value using the given formatter. Read more
Default
#![allow(unused)] fn main() { fn default() -> ContextManager<K, V, S> }Creates an empty context manager
Extend<(K, V)>
K: Hash + Eq, S: BuildHasher + Default
#![allow(unused)] fn main() { fn extend<I: IntoIterator<Item = (K, V)>>(&mut self, iter: I) }Adds key-value pairs from an iterator to the context manager.
If the context manager is empty, a new HashMap<K, V, S> is created with the default hasher.
#![allow(unused)] fn main() { fn extend_one(&mut self, item: A) }🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
#![allow(unused)] fn main() { fn extend_reserve(&mut self, additional: usize) }🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
From<[(K, V); N]>
K: Hash + Eq
#![allow(unused)] fn main() { fn from(initial: [(K, V); N]) -> ContextManager<K, V, RandomState> }Creates a new context manager with a first context initialized from the key-value pairs in initial.
Repeated keys will have all but one of the values dropped.
From
K: Hash + Eq, S: BuildHasher
#![allow(unused)] fn main() { fn from(initial: [HashMap<K, V, S>; N]) -> ContextManager<K, V, S> fn from(initial: HashMap<K, V, S>) -> ContextManager<K, V, S> }Creates a new context manager initialized with the contexts in initial.
Precedence proceeds from the first context toward the last.
FromIterator<(K, V)>
K: Hash + Eq, S: BuildHasher + Default
#![allow(unused)] fn main() { fn from_iter<I: IntoIterator<Item = (K, V)>>(initial: I) -> ContextManager<K, V, S> }Creates a new context manager with a first context initialized from the key-value pairs in initial.
Repeated keys will have all but one of the values dropped.
FromIterator<HashMap<K, V, S>>
K: Hash + Eq, S: BuildHasher
#![allow(unused)] fn main() { fn from_iter<I: IntoIterator<Item = HashMap<K, V, S>>>(iter: I) -> Self }Creates a new context manager initialized with the contexts in initial.
Precedence proceeds from the first context toward the last.
Index<&Q>
K: Hash + Eq + Borrow<Q>, Q: ?Sized + Hash + Eq, S: BuildHasher
#![allow(unused)] fn main() { type Output = V }The returned type after indexing.
#![allow(unused)] fn main() { fn index(&self, key: &Q) -> &V }Returns a reference to the value associated with key.
Panics if the context manager is empty, or key is not found in any contexts.
PartialEq
K: Hash + Eq, V: PartialEq, S: BuildHasher
#![allow(unused)] fn main() { fn eq(&self, other: &ContextManager<K, V, S>) -> bool }Tests for self and other values to be equal, and is used by ==.
#![allow(unused)] fn main() { const fn ne(&self, other: &Rhs) -> bool }Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Eq
K: Hash + Eq, V: Eq, S: BuildHasher