Struct Enigma

Source
pub struct Enigma<R, P, O, const ROTOR_NUM: usize = DEFAULT_ROTOR_NUM> { /* private fields */ }
Expand description

Enigma Finite State Machine

An incomplete instance (e.g. from Enigma::default) can be initialised later semi-dynamically with the compiler checking correct initialisation before use.

§Examples

let enigma = Enigma::default()
    .set_rotors(Wheel::new([ROTOR_I, ROTOR_II, ROTOR_III]))
    .set_plugboard(Plugboard::new(&[]));

let mut enigma = enigma.set_reflector(REFLECTOR_A);

enigma.encrypt_letter(Letter::A);

The compiler can detect if one of the required initialisation steps is missing:

let mut enigma = Enigma::default()
    .set_rotors(Wheel::new([ROTOR_I, ROTOR_II, ROTOR_III]))
    .set_plugboard(Plugboard::new(&[]));

enigma.encrypt_letter(Letter::A); // encrypt can't be called until a reflector is set

Implementations§

Source§

impl Enigma<Unset, Unset, Unset>

Source

pub fn new<const ROTOR_NUM: usize>( config: &EnigmaSettings<ROTOR_NUM>, ) -> Enigma<Wheel<ROTOR_NUM>, Plugboard, Reflector, ROTOR_NUM>

Create a fully setup Enigma FSM from the provided settings

§Examples
let mut enigma = Enigma::new(&EnigmaSettings {
    rotors: [ROTOR_II, ROTOR_IV, ROTOR_V],
    plugboard: Plugboard::from_str(
        "AV,BS,CG,DL,FU,HZ,IN,KM,OW,RX",
    ).unwrap(),
    reflector: REFLECTOR_B,
    ringstellung: [2, 21, 12],
    grundstellung: [Letter::B, Letter::L, Letter::A],
});
enigma.encrypt_letter(Letter::A);

Rust will attempt to determine whether to create an M3 or M4 (or another variant) based on the size of the rotor array. However, in some cases, it may be necessary to specify this explicitly:


let settings = EnigmaSettings {
    rotors: [ROTOR_II, ROTOR_IV, ROTOR_V, ROTOR_III],
    plugboard: Plugboard::from_str(
        "AV,BS,CG,DL,FU,HZ,IN,KM,OW,RX",
    ).unwrap(),
    reflector: REFLECTOR_B_THIN,
    ringstellung: [2, 21, 12, 3],
    grundstellung: [Letter::B, Letter::L, Letter::A, Letter::B],
};

let mut enigma = Enigma::new::<M4>(&settings);
enigma.encrypt_letter(Letter::A);

As alluded to above this also lets you create instances of machines that never existed. For example, a 10-rotor enigma machine can be created with Enigma::new<10>(&settings)

Source§

impl<AnyRotor, AnyPlugboard, AnyReflector, const ROTOR_NUM: usize> Enigma<AnyRotor, AnyPlugboard, AnyReflector, ROTOR_NUM>

Source

pub fn set_rotors( self, rotors: Wheel, ) -> Enigma<Wheel, AnyPlugboard, AnyReflector, ROTOR_NUM>

Set the Enigma rotors

Source

pub fn set_plugboard( self, plugboard: Plugboard, ) -> Enigma<AnyRotor, Plugboard, AnyReflector, ROTOR_NUM>

Set the Enigma plguboard

Source

pub fn set_reflector( self, reflector: Reflector, ) -> Enigma<AnyRotor, AnyPlugboard, Reflector, ROTOR_NUM>

Set the Enigma reflector

Source§

impl<AnyPlugboard, AnyReflector, const ROTOR_NUM: usize> Enigma<Wheel<ROTOR_NUM>, AnyPlugboard, AnyReflector, ROTOR_NUM>

Source

pub fn set_grundstellung( &mut self, positions: [Letter; ROTOR_NUM], ) -> &mut Enigma<Wheel<ROTOR_NUM>, AnyPlugboard, AnyReflector, ROTOR_NUM>

Set the starting position for the rotors

Source

pub fn set_ringstellung( &mut self, positions: [u8; ROTOR_NUM], ) -> &mut Enigma<Wheel<ROTOR_NUM>, AnyPlugboard, AnyReflector, ROTOR_NUM>

Set the ring settings for the rotors

Source

pub fn get_grundstellung(&self) -> [Letter; ROTOR_NUM]

Get Rotor positions

Source§

impl<const ROTOR_NUM: usize> Enigma<Wheel<ROTOR_NUM>, Plugboard, Reflector, ROTOR_NUM>

Source

pub fn encrypt_letter(&mut self, letter: Letter) -> Letter

Encrypt/decrypt a Letter and advance the state

Source

pub fn encrypt_char(&mut self, letter: char) -> char

Encrypt/decrypt a char and advance the state

Trait Implementations§

Source§

impl<R: Clone, P: Clone, O: Clone, const ROTOR_NUM: usize> Clone for Enigma<R, P, O, ROTOR_NUM>

Source§

fn clone(&self) -> Enigma<R, P, O, ROTOR_NUM>

Returns a duplicate 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<R: Debug, P: Debug, O: Debug, const ROTOR_NUM: usize> Debug for Enigma<R, P, O, ROTOR_NUM>

Source§

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

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

impl Default for Enigma<Unset, Unset, Unset, DEFAULT_ROTOR_NUM>

Source§

fn default() -> Self

Creates a default Enigma state machine where all parameters are unset.

This function will always create an M3 variant Enigma.

The configuration can be set later in execution using the various setter functions.

Source§

impl<R: Hash, P: Hash, O: Hash, const ROTOR_NUM: usize> Hash for Enigma<R, P, O, ROTOR_NUM>

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<R: Ord, P: Ord, O: Ord, const ROTOR_NUM: usize> Ord for Enigma<R, P, O, ROTOR_NUM>

Source§

fn cmp(&self, other: &Enigma<R, P, O, ROTOR_NUM>) -> 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<R: PartialEq, P: PartialEq, O: PartialEq, const ROTOR_NUM: usize> PartialEq for Enigma<R, P, O, ROTOR_NUM>

Source§

fn eq(&self, other: &Enigma<R, P, O, ROTOR_NUM>) -> 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<R: PartialOrd, P: PartialOrd, O: PartialOrd, const ROTOR_NUM: usize> PartialOrd for Enigma<R, P, O, ROTOR_NUM>

Source§

fn partial_cmp(&self, other: &Enigma<R, P, O, ROTOR_NUM>) -> 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<R: Copy, P: Copy, O: Copy, const ROTOR_NUM: usize> Copy for Enigma<R, P, O, ROTOR_NUM>

Source§

impl<R: Eq, P: Eq, O: Eq, const ROTOR_NUM: usize> Eq for Enigma<R, P, O, ROTOR_NUM>

Source§

impl<R, P, O, const ROTOR_NUM: usize> StructuralPartialEq for Enigma<R, P, O, ROTOR_NUM>

Auto Trait Implementations§

§

impl<R, P, O, const ROTOR_NUM: usize> Freeze for Enigma<R, P, O, ROTOR_NUM>
where R: Freeze, P: Freeze, O: Freeze,

§

impl<R, P, O, const ROTOR_NUM: usize> RefUnwindSafe for Enigma<R, P, O, ROTOR_NUM>

§

impl<R, P, O, const ROTOR_NUM: usize> Send for Enigma<R, P, O, ROTOR_NUM>
where R: Send, P: Send, O: Send,

§

impl<R, P, O, const ROTOR_NUM: usize> Sync for Enigma<R, P, O, ROTOR_NUM>
where R: Sync, P: Sync, O: Sync,

§

impl<R, P, O, const ROTOR_NUM: usize> Unpin for Enigma<R, P, O, ROTOR_NUM>
where R: Unpin, P: Unpin, O: Unpin,

§

impl<R, P, O, const ROTOR_NUM: usize> UnwindSafe for Enigma<R, P, O, ROTOR_NUM>
where R: UnwindSafe, P: UnwindSafe, O: UnwindSafe,

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