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

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 + PartialOrd,

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

impl PartialEq<BitString> for BitStringAsn1

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method 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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method 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

This method 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

This method 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

This method 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

This method 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§

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

🔬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,

§

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>,

§

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>,

§

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>,