aoc19intcode

Struct IntcodeVM

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

A VM that runs Intcode programs.

§Examples

use aoc19intcode::IntcodeVM;
assert_eq!(
    IntcodeVM::from_prog(&[2, 4, 4, 5, 99, 0])
        .run_prog()
        .expect("Tried to run a halted program.")[5],
    9801
);
use std::{thread, sync::mpsc};
use aoc19intcode::IntcodeVM;
let input = [2, 4, 4, 5, 99, 0];
let (mtx, prx) = mpsc::channel();
let (ptx, mrx) = mpsc::channel();
let (ftx, frx) = mpsc::channel();
mtx.send(2).expect("Failed to send an input to the VM.");
thread::spawn(move || {
    IntcodeVM::with_inp_flag(&input, Some(prx), Some(ptx), Some(ftx))
        .run_prog()
});
for _ in frx.recv() {
    println!("received {}", mrx.recv().expect("VM disconnected."));
}

Implementations§

Source§

impl IntcodeVM

Source

pub fn from_prog(prog: &[i128]) -> Self

Returns a new VM with a program and no I/O controls.

Source

pub fn with_io( prog: &[i128], input: Option<Receiver<i128>>, output: Option<Sender<i128>>, ) -> Self

Returns a new VM with a program and I/O controls.

Source

pub fn with_inp_flag( prog: &[i128], input: Option<Receiver<i128>>, output: Option<Sender<i128>>, inp_wait_flag: Option<Sender<()>>, ) -> Self

Returns a new VM with a program and I/O controls, includes an input waiting flag sender that sends a unit (()) when the VM is expecting input.

Source

pub fn run_prog(&mut self) -> Result<Vec<i128>, RanHaltedVMError>

Runs the VM.

Trait Implementations§

Source§

impl Debug for IntcodeVM

Source§

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

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

impl Default for IntcodeVM

Source§

fn default() -> Self

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

impl Index<usize> for IntcodeVM

Source§

fn index(&self, ix: usize) -> &Self::Output

To access any cell’s value.

Source§

type Output = i128

The returned type after indexing.
Source§

impl IndexMut<usize> for IntcodeVM

Source§

fn index_mut(&mut self, ix: usize) -> &mut Self::Output

To mutably access any cell’s value.

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