Skip to main content

Dodecet

Struct Dodecet 

Source
pub struct Dodecet { /* private fields */ }
Expand description

A 12-bit dodecet value (0-4095)

§Example

use dodecet_encoder::Dodecet;

let d = Dodecet::from_hex(0xABC);
assert_eq!(d.value(), 0xABC);
assert_eq!(d.nibble(0).unwrap(), 0xC);
assert_eq!(d.nibble(1).unwrap(), 0xB);
assert_eq!(d.nibble(2).unwrap(), 0xA);

Implementations§

Source§

impl Dodecet

Source

pub fn new(value: u16) -> Result<Self>

Create a new dodecet from a u16 value

§Errors

Returns DodecetError::Overflow if value > 4095

§Example
use dodecet_encoder::Dodecet;

let d = Dodecet::new(0xABC).unwrap();
assert_eq!(d.value(), 0xABC);
Source

pub unsafe fn from_hex_unchecked(value: u16) -> Self

Create a dodecet from a hex value (unchecked)

§Safety

Caller must ensure value <= 4095

§Example
use dodecet_encoder::Dodecet;

let d = unsafe { Dodecet::from_hex_unchecked(0xABC) };
assert_eq!(d.value(), 0xABC);
Source

pub const fn from_hex(value: u16) -> Self

Create a dodecet from a hex value

§Example
use dodecet_encoder::Dodecet;

let d = Dodecet::from_hex(0xABC);
assert_eq!(d.value(), 0xABC);
Source

pub fn from_signed(value: i16) -> Self

Create a dodecet from a signed i16 value (-2048 to 2047)

§Example
use dodecet_encoder::Dodecet;

let d = Dodecet::from_signed(-100);
assert_eq!(d.as_signed(), -100);
Source

pub fn value(self) -> u16

Get the raw value

§Example
use dodecet_encoder::Dodecet;

let d = Dodecet::from_hex(0xABC);
assert_eq!(d.value(), 0xABC);
Source

pub fn nibble(self, index: u8) -> Result<u8>

Get a specific nibble (0, 1, or 2)

§Arguments
  • index - Nibble index (0 = LSB, 2 = MSB)
§Example
use dodecet_encoder::Dodecet;

let d = Dodecet::from_hex(0xABC);
assert_eq!(d.nibble(0).unwrap(), 0xC);
assert_eq!(d.nibble(1).unwrap(), 0xB);
assert_eq!(d.nibble(2).unwrap(), 0xA);
Source

pub fn set_nibble(&mut self, index: u8, nibble: u8) -> Result<()>

Set a specific nibble

§Arguments
  • index - Nibble index (0 = LSB, 2 = MSB)
  • nibble - New nibble value (0-15)
§Example
use dodecet_encoder::Dodecet;

let mut d = Dodecet::from_hex(0xABC);
d.set_nibble(0, 0xD).unwrap();
assert_eq!(d.value(), 0xABD);
Source

pub fn is_zero(self) -> bool

Check if dodecet is zero

§Example
use dodecet_encoder::Dodecet;

assert!(Dodecet::from_hex(0).is_zero());
assert!(!Dodecet::from_hex(0xABC).is_zero());
Source

pub fn is_max(self) -> bool

Check if dodecet is at maximum value

§Example
use dodecet_encoder::Dodecet;

assert!(Dodecet::from_hex(0xFFF).is_max());
assert!(!Dodecet::from_hex(0xABC).is_max());
Source

pub fn count_ones(self) -> u32

Count set bits (population count)

§Example
use dodecet_encoder::Dodecet;

let d = Dodecet::from_hex(0xFFF); // All bits set
assert_eq!(d.count_ones(), 12);
Source

pub fn count_zeros(self) -> u32

Count unset bits

§Example
use dodecet_encoder::Dodecet;

let d = Dodecet::from_hex(0x000); // No bits set
// Note: count_zeros() operates on underlying u16, returns 16 for 0x000
Source

pub fn and(self, other: Dodecet) -> Dodecet

Bitwise AND

Source

pub fn or(self, other: Dodecet) -> Dodecet

Bitwise OR

Source

pub fn xor(self, other: Dodecet) -> Dodecet

Bitwise XOR

Source

pub fn not(self) -> Dodecet

Bitwise NOT

Source

pub fn wrapping_add(self, other: Dodecet) -> Dodecet

Arithmetic addition with wrapping

Source

pub fn wrapping_sub(self, other: Dodecet) -> Dodecet

Arithmetic subtraction with wrapping

Source

pub fn wrapping_mul(self, other: Dodecet) -> Dodecet

Arithmetic multiplication with wrapping

Source

pub fn to_hex_string(self) -> String

Convert to hex string (3 characters)

§Example
use dodecet_encoder::Dodecet;

let d = Dodecet::from_hex(0xABC);
assert_eq!(d.to_hex_string(), "ABC");
Source

pub fn from_hex_str(s: &str) -> Result<Self>

Parse from hex string

§Example
use dodecet_encoder::Dodecet;

let d = Dodecet::from_hex_str("ABC").unwrap();
assert_eq!(d.value(), 0xABC);
Source

pub fn to_binary_string(self) -> String

Convert to binary string (12 characters)

§Example
use dodecet_encoder::Dodecet;

let d = Dodecet::from_hex(0xABC);
assert_eq!(d.to_binary_string(), "101010111100");
Source

pub fn as_signed(self) -> i16

Geometric interpretation: Treat as signed value (-2048 to 2047)

§Example
use dodecet_encoder::Dodecet;

let d = Dodecet::from_hex(0x800);
assert_eq!(d.as_signed(), -2048);
Source

pub fn normalize(self) -> f64

Normalize to floating point [0.0, 1.0]

§Example
use dodecet_encoder::Dodecet;

let d = Dodecet::from_hex(0x800); // Midpoint
assert!((d.normalize() - 0.5).abs() < 0.001);

Trait Implementations§

Source§

impl Add for Dodecet

Source§

type Output = Dodecet

The resulting type after applying the + operator.
Source§

fn add(self, other: Self) -> Self::Output

Performs the + operation. Read more
Source§

impl Binary for Dodecet

Source§

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

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

impl BitAnd for Dodecet

Source§

type Output = Dodecet

The resulting type after applying the & operator.
Source§

fn bitand(self, other: Self) -> Self::Output

Performs the & operation. Read more
Source§

impl BitOr for Dodecet

Source§

type Output = Dodecet

The resulting type after applying the | operator.
Source§

fn bitor(self, other: Self) -> Self::Output

Performs the | operation. Read more
Source§

impl BitXor for Dodecet

Source§

type Output = Dodecet

The resulting type after applying the ^ operator.
Source§

fn bitxor(self, other: Self) -> Self::Output

Performs the ^ operation. Read more
Source§

impl Clone for Dodecet

Source§

fn clone(&self) -> Dodecet

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Dodecet

Source§

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

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

impl Default for Dodecet

Source§

fn default() -> Dodecet

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

impl Display for Dodecet

Source§

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

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

impl From<Dodecet> for u16

Source§

fn from(d: Dodecet) -> Self

Converts to this type from the input type.
Source§

impl From<u8> for Dodecet

Source§

fn from(value: u8) -> Self

Converts to this type from the input type.
Source§

impl Hash for Dodecet

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 Mul for Dodecet

Source§

type Output = Dodecet

The resulting type after applying the * operator.
Source§

fn mul(self, other: Self) -> Self::Output

Performs the * operation. Read more
Source§

impl Not for Dodecet

Source§

type Output = Dodecet

The resulting type after applying the ! operator.
Source§

fn not(self) -> Self::Output

Performs the unary ! operation. Read more
Source§

impl Octal for Dodecet

Source§

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

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

impl Ord for Dodecet

Source§

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

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

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

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

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

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

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

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

impl PartialEq for Dodecet

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · 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 Dodecet

Source§

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

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · 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 (const: unstable) · 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 (const: unstable) · 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 (const: unstable) · 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 Sub for Dodecet

Source§

type Output = Dodecet

The resulting type after applying the - operator.
Source§

fn sub(self, other: Self) -> Self::Output

Performs the - operation. Read more
Source§

impl TryFrom<u16> for Dodecet

Source§

type Error = DodecetError

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

fn try_from(value: u16) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl Copy for Dodecet

Source§

impl Eq for Dodecet

Source§

impl StructuralPartialEq for Dodecet

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, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.