picky_asn1::bit_string

Struct 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

Source

pub fn as_bytes(&self) -> &[u8]

Returns inner bytes slice

Source

pub fn with_len(num_bits: usize) -> BitString

Construct a BitString of length n with all bits set to 0.

Source

pub fn with_bytes_and_len<V>(data: V, num_bits: usize) -> BitString
where 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);
Source

pub fn with_bytes<V>(data: V) -> BitString
where 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);
Source

pub fn get_num_bits(&self) -> usize

Get the number of available bits in the BitString

Source

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);
Source

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);
Source

pub fn set(&mut self, i: usize, val: bool)

Set bit i to val.

Source

pub fn get_num_unused_bits(&self) -> u8

Source

pub fn get_num_buckets(&self) -> usize

Source

pub fn get_bucket(&self, i: usize) -> u8

Source

pub fn get_bucket_mut(&mut self, i: usize) -> &mut u8

Source

pub fn set_bucket(&mut self, i: usize, value: u8)

Source

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]);
Source

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 Clone for BitString

Source§

fn clone(&self) -> BitString

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for BitString

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for BitString

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for BitString

Source§

fn deserialize<D>(deserializer: D) -> Result<BitString, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl From<BitString> for BitStringAsn1

Source§

fn from(wrapped: BitString) -> Self

Converts to this type from the input type.
Source§

impl From<BitString> for Vec<u8>

Source§

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

Source§

fn from(wrapper: BitStringAsn1) -> BitString

Converts to this type from the input type.
Source§

impl Hash for BitString

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Ord for BitString

Source§

fn cmp(&self, other: &BitString) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq<BitString> for BitStringAsn1

Source§

fn eq(&self, other: &BitString) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq for BitString

Source§

fn eq(&self, other: &BitString) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for BitString

Source§

fn partial_cmp(&self, other: &BitString) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Serialize for BitString

Source§

fn serialize<S>( &self, serializer: S, ) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Eq for BitString

Source§

impl StructuralPartialEq for BitString

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,