Struct picky_asn1::bit_string::BitString
source · 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) -> BitStringwhere
V: Into<Vec<u8>>,
pub fn with_bytes_and_len<V>(data: V, num_bits: usize) -> BitStringwhere V: Into<Vec<u8>>,
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) -> BitStringwhere
V: Into<Vec<u8>>,
pub fn with_bytes<V>(data: V) -> BitStringwhere V: Into<Vec<u8>>,
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]);
source§impl From<BitStringAsn1> for BitString
impl From<BitStringAsn1> for BitString
source§fn from(wrapper: BitStringAsn1) -> BitString
fn from(wrapper: BitStringAsn1) -> BitString
source§impl Ord for BitString
impl Ord for BitString
source§impl PartialEq<BitString> for BitString
impl PartialEq<BitString> for BitString
source§impl PartialEq<BitString> for BitStringAsn1
impl PartialEq<BitString> for BitStringAsn1
source§impl PartialOrd<BitString> for BitString
impl PartialOrd<BitString> for BitString
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more