Rust

A simple rust example can be found below.

use chronoptics_tof as tof;
use tof::Camera;

fn example() -> tof::Result<()> {
    let mut cam = tof::KeaCamera::new_simple("2020004")?;
    let nr_streams = tof::select_streams(&cam, &[tof::FrameType::Z])?;
    cam.start()?;

    for _ in 0..100 {
        let mut frames = cam.get_frames()?;

        for frame in &mut frames {
            match frame.frame_type()? {
                tof::FrameType::Z => {
                    // Access const data
                    let _data = frame.as_slice::<f32>()?;

                    // Access mut data
                    let _data = frame.as_mut_slice::<f32>()?;
                }
                _ => println!("Unhandled frame type"),
            }
        }
    }

    cam.stop()?;

    Ok(())
}

fn main() {
    let ret = example();

    match ret {
        Ok(_) => println!("Stream frames ran succesfully"),
        Err(err) => println!("Failed with {err}"),
    }
}

Error handling in rust is done through the default std::result::Result enum. The tof::Error is just a wrapper around the dynamic error version.

pub type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;

Data can both be accessed safely and unsafely, the safe version does additional checks, so has a slight run time performance cost. To access it safely, call as_slice and as_mut_slice. To access it unsafely, call as_slice_unchecked and as_mut_slice_unchecked.