pub struct BitString { /* private fields */ }
Expand description
A bit string.
Rewrite based on this implementation by Melvin Walls Jr. licensed with
The MIT License (MIT)
Copyright (c) 2016 Melvin Walls Jr.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
§Examples
use picky_asn1::bit_string::BitString;
let mut b = BitString::with_len(60);
b.set(0, true);
assert_eq!(b.is_set(0), true);
b.set(59, true);
assert_eq!(b.is_set(59), true);
// because len is 60, attempts at setting anything greater than 59 won't change anything
b.set(63, true);
assert_eq!(b.is_set(63), false);
Implementations§
Source§impl BitString
impl BitString
Sourcepub fn with_len(num_bits: usize) -> BitString
pub fn with_len(num_bits: usize) -> BitString
Construct a BitString
of length n
with all bits set to 0.
Sourcepub fn with_bytes_and_len<V>(data: V, num_bits: usize) -> BitString
pub fn with_bytes_and_len<V>(data: V, num_bits: usize) -> BitString
Construct a BitString
of length n
with initial values contained in data
.
§Examples
use picky_asn1::bit_string::BitString;
let v: Vec<u8> = vec![0x00, 0x02];
let b = BitString::with_bytes_and_len(v, 15);
assert_eq!(b.is_set(0), false);
assert_eq!(b.is_set(14), true);
// because len is 15, everything greater than 14 will returns false
assert_eq!(b.is_set(15), false);
assert_eq!(b.is_set(938), false);
Sourcepub fn with_bytes<V>(data: V) -> BitString
pub fn with_bytes<V>(data: V) -> BitString
Construct a BitString
from initial values contained in data
.
Length is inferred fromthe size of data
.
§Examples
use picky_asn1::bit_string::BitString;
let v: Vec<u8> = vec![0x00, 0x02];
let b = BitString::with_bytes(v);
assert_eq!(b.is_set(0), false);
assert_eq!(b.is_set(14), true);
Sourcepub fn get_num_bits(&self) -> usize
pub fn get_num_bits(&self) -> usize
Get the number of available bits in the BitString
Sourcepub fn set_num_bits(&mut self, num_bits: usize)
pub fn set_num_bits(&mut self, num_bits: usize)
Set the length of a BitString
with each additional slot filled with 0.
§Examples
use picky_asn1::bit_string::BitString;
let v: Vec<u8> = vec![0x01, 0x01];
let mut b = BitString::with_bytes_and_len(v, 16);
assert_eq!(b.is_set(7), true);
assert_eq!(b.is_set(15), true);
b.set_num_bits(8);
assert_eq!(b.is_set(7), true);
b.set(15, true); // attempts to set a value out of the bounds are ignored
assert_eq!(b.is_set(15), false);
b.set_num_bits(16);
assert_eq!(b.is_set(7), true);
assert_eq!(b.is_set(15), false);
b.set(15, true);
assert_eq!(b.is_set(15), true);
Sourcepub fn is_set(&self, i: usize) -> bool
pub fn is_set(&self, i: usize) -> bool
Check if bit i
is set.
§Examples
use picky_asn1::bit_string::BitString;
let mut b = BitString::with_len(10);
assert_eq!(b.is_set(7), false);
b.set(7, true);
assert_eq!(b.is_set(7), true);
pub fn get_num_unused_bits(&self) -> u8
pub fn get_num_buckets(&self) -> usize
pub fn get_bucket(&self, i: usize) -> u8
pub fn get_bucket_mut(&mut self, i: usize) -> &mut u8
pub fn set_bucket(&mut self, i: usize, value: u8)
Sourcepub fn payload_view(&self) -> &[u8] ⓘ
pub fn payload_view(&self) -> &[u8] ⓘ
Returns an immutabe view on the payload.
§Examples
use picky_asn1::bit_string::BitString;
let v: Vec<u8> = vec![0x01, 0x00];
let mut b = BitString::with_bytes_and_len(v, 15);
b.set(14, true);
let payload = b.payload_view();
assert_eq!(payload, &[0x01, 0x02]);
Sourcepub fn payload_view_mut(&mut self) -> &mut [u8] ⓘ
pub fn payload_view_mut(&mut self) -> &mut [u8] ⓘ
Returns a mutabe view on the payload.
§Examples
use picky_asn1::bit_string::BitString;
let v: Vec<u8> = vec![0x01, 0x00];
let mut b = BitString::with_bytes_and_len(v, 15);
b.set(14, true);
let payload = b.payload_view_mut();
payload[0] = 0x20;
assert_eq!(payload, &[0x20, 0x02]);
Trait Implementations§
Source§impl<'de> Deserialize<'de> for BitString
impl<'de> Deserialize<'de> for BitString
Source§fn deserialize<D>(deserializer: D) -> Result<BitString, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<BitString, D::Error>where
D: Deserializer<'de>,
Source§impl From<BitString> for BitStringAsn1
impl From<BitString> for BitStringAsn1
Source§impl From<BitString> for Vec<u8>
impl From<BitString> for Vec<u8>
Source§fn from(bs: BitString) -> Self
fn from(bs: BitString) -> Self
Strips ‘unused bits count’ byte and returns payload.
§Examples
use picky_asn1::bit_string::BitString;
let v: Vec<u8> = vec![0x01, 0x00];
let mut b = BitString::with_bytes_and_len(v, 15);
b.set(14, true);
let payload: Vec<u8> = b.into();
assert_eq!(payload, vec![0x01, 0x02]);