Struct Unpacker

Source
pub struct Unpacker<const S: usize> { /* private fields */ }
Expand description

A struct for that unpacks bytes and emits lines of gcode.

Implementations§

Source§

impl<const S: usize> Unpacker<S>

Source

pub fn unpack(&mut self, byte: &u8) -> Result<MeatPackResult<'_>, MeatPackError>

Examples found in repository?
examples/unpack.rs (line 17)
5fn main() {
6	let packed: [u8; 93] = [
7		255, 255, 251, 255, 255, 247, 255, 255, 250, 59, 32, 10, 255, 255, 251, 127, 77, 243, 32,
8		15, 80, 255, 32, 82, 195, 127, 77, 243, 32, 15, 81, 255, 32, 83, 195, 47, 77, 16, 239, 32,
9		4, 0, 255, 32, 89, 4, 0, 255, 32, 90, 2, 240, 32, 43, 5, 192, 47, 77, 48, 239, 32, 3, 240,
10		32, 63, 89, 0, 255, 32, 90, 4, 191, 32, 1, 192, 47, 77, 64, 255, 32, 80, 4, 0, 255, 32, 82,
11		33, 0, 255, 32, 84, 4, 0,
12	];
13
14	let mut unpacker = Unpacker::<64>::default();
15
16	for b in packed.iter() {
17		let res = unpacker.unpack(b);
18		match res {
19			Ok(MeatPackResult::WaitingForNextByte) => {
20				//println!("Waiting for next byte");
21			}
22			Ok(MeatPackResult::Line(line)) => {
23				let line = str::from_utf8(line).unwrap();
24				println!("{:?}", line);
25			}
26			Err(e) => {
27				println!("{:?}", e);
28				panic!();
29			}
30		}
31	}
32}
More examples
Hide additional examples
examples/pack_unpack.rs (line 33)
5fn main() {
6	let gcode = "M73 P0 R3
7M73 Q0 S3 ; Hello
8M201 X4000 Y4000 Z200 E2500
9M203 X300 Y300 Z40 E100
10M204 P4000 R1200 T4000
11";
12	let mut packer = Packer::<64>::default();
13	let mut out: Vec<u8> = vec![];
14
15	out.extend(packer.header());
16
17	for byte in gcode.as_bytes() {
18		let packed = packer.pack(byte);
19		match packed {
20			Ok(MeatPackResult::Line(line)) => {
21				println!("{:?}", line);
22				out.extend(line);
23			}
24			Ok(MeatPackResult::WaitingForNextByte) => {}
25			Err(e) => println!("{:?}", e),
26		}
27	}
28
29	println!("{:?}", out);
30
31	let mut unpacker = Unpacker::<64>::default();
32	for byte in out {
33		let res = unpacker.unpack(&byte);
34		match res {
35			Ok(MeatPackResult::WaitingForNextByte) => {}
36			Ok(MeatPackResult::Line(line)) => {
37				// If in std.
38				for byte in line {
39					let c = char::from(*byte);
40					print!("{}", c);
41				}
42			}
43			Err(e) => {
44				println!("{:?}", e);
45				process::exit(0)
46			}
47		}
48	}
49}

Trait Implementations§

Source§

impl<const S: usize> Default for Unpacker<S>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<const S: usize> Freeze for Unpacker<S>

§

impl<const S: usize> RefUnwindSafe for Unpacker<S>

§

impl<const S: usize> Send for Unpacker<S>

§

impl<const S: usize> Sync for Unpacker<S>

§

impl<const S: usize> Unpin for Unpacker<S>

§

impl<const S: usize> UnwindSafe for Unpacker<S>

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.