Struct aoc::y2019::intcode::Program

source ·
pub struct Program { /* private fields */ }
Expand description

A program that operates on a set of commands which represent Intcodes.

Implementations§

source§

impl Program

source

pub fn new(cmds: Vec<i64>, start_params: &[i64]) -> Self

Create a new program that operates on the provided commands. Start parameters can be given as well.

source

pub fn run(&mut self, params: &[i64]) -> Result<i64>

Run one cycle of the program. A cycle is finished when either the position is at the end of all commands, or an output (4) or exit (99) command is reached. In general this function should be repeatedly called until is_finished returns true.

The program can still be run even if it’s already finished, but the output will always be zero.

Example
use aoc::y2019::intcode::{parse_input, Program};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut program = Program::new(parse_input("3,5,4,5,99,0")?, &[1]);

    assert_eq!(1, program.run(&[])?);
    assert!(program.is_finished());

    assert_eq!(0, program.run(&[])?); // Already finished

    Ok(())
}
source

pub fn is_finished(&self) -> bool

Check whether this program is still executable or reached the end of execution. The end is reached when either the current position is at the end of commands or the current command is the exit (99) code.

source

pub fn cmds(self) -> Vec<i64>

Return the current state of the commands this program runs. This will consume the program.

Trait Implementations§

source§

impl Default for Program

source§

fn default() -> Program

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

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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.

§

impl<T> Pointable for T

§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T> Same<T> for T

§

type Output = T

Should always be Self
source§

impl<T, U> TryFrom<U> for Twhere 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 Twhere 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.