Author Archives: frehberg

Rust Function implementing Generator

The following example demonstrates how to implement a Rust-function implementing a generator (unstable feature in nightly compiler)

#![feature(generators, generator_trait)]
use std::ops::{Generator, GeneratorState};
use std::pin::Pin;

fn generator() -> impl Generator<
Yield=(),
Return=()>
{
   let generator = || {
      println!("2");
      yield;
      println!("4");
    };
   generator
}

#[derive(Debug, Clone)]
enum MyError {
   Fault
}

fn generator2() -> impl Generator<
   Yield=Result<u32, MyError>,
   Return=&'static str>
{
   let generator = || {
      yield Ok(1);
      yield Err(MyError::Fault);
      return "foo";
   };
   generator
}

fn main() {
   let mut generator = generator();

   println!("1");
   Pin::new(&mut generator).resume(());
   println!("3");
   Pin::new(&mut generator).resume(());
   println!("5");

   println!("-------");

   let mut generator2 = generator2();

   match Pin::new(&mut generator2).resume(()) {
      GeneratorState::Yielded(val) => { println!("{:?}", val); }
      _ => panic!("unexpected value from resume"),
   }

   match Pin::new(&mut generator2).resume(()) {
      GeneratorState::Yielded(val) => { println!  ("{:?}", val); }
      _ => panic!("unexpected value from resume"),
   }

   match Pin::new(&mut generator2).resume(()) {
      GeneratorState::Complete(val) => { println!("{}", val); }
      _ => panic!("unexpected value from resume"),
   }
}

LibreOffice – Import Cisco Network Icons to Gallery

Cisco Network Icons are available for download as Zip-file (Use the Search Engine of your choice to find this Zip-file), containing the icons as EPS. We want to use these icons in LibreOffice to create network diagrams.

Each of these EPS-files contains a preview TIFF image of low resolution, that would be used for import into LibreOffice-Gallery. Just, these TIFF images would result in diagrams of low quality. Instead one should convert each EPS-file into a high-quality, scalable SVG-file, being imported to the LibreOffice Gallery.

So, after unzipping all EPS-files into a folder, use the following script to iterate all EPS-files of the local folder, to convert each EPS-file to a high-quality, scalable SVG.

Finally, use “New Theme” of the “Gallery” to create a new theme and import all SVG-icons to it. Now, when you add such icon to your diagram, additional glue-points can be added to each icon to connect “Connectors” to it (lines following the icon when being moved around)

for epsfile in *.eps; do 
   echo "${epsfile}"
   ps2pdf -dEPSCrop "${epsfile}" - | pdftocairo -svg - "${epsfile}.svg"
done

ChaosCamp 2019

Attending the Chaos Camp 2019 as speaker was quite an experience. I did a talk about Secure Architecture of IoT Devices https://fahrplan.events.ccc.de/camp/2019/Fahrplan/events/10268.html

UTest Harness for Rust (Setup/Test/Release)

Edit Titel should have been “setup/test/teardown”

The crate test-generator-utest implements a 3-phase utest-harness for Rust. These 3 phases are:

Setup: the setup function must initialize the context; in case the setup-function panics/aborts, the utest is aborted
fn() -> Context

Test: if the setup did return with valud context item, the context-reference is borowed to invoke the test-function
fn( & Context )

Teardown: no matter if the test-function above did panic/abort, the teardown function is invoked to release the context
fn( Context )

No matter of any panic or failing assertion in the second phase (feature testing), the teardown function is invoked. The test will either succeed, or otherwise unwinding a failure in the setup-phase, the test-phase or the teardown-phase.

This crate has been inspired by the post of Eric Opines.

## Usage

Please see the executable example mytests

#[cfg(test)]
extern crate test_generator_utest;

// demonstrating usage of utest-harness
mod testsuite {
    use std::fs::File;
    use std::io::prelude::*;

    use test_generator_utest::utest;

    // Defining a context structure, storing the resources
    struct Context<'t> { file: File, name: &'t str }

    // Setup - Initializing the resources
    fn setup<'t>(filename: &str) -> Context {
        // unwrap may panic
        Context { file: File::create(filename).unwrap(), name: filename }
    }

    // Teardown - Releasing the resources
    fn teardown(context: Context) {
        let Context { file, name } = context;

        // drop file resources
        std::mem::drop(file);

        // unwrap may panic
        std::fs::remove_file(name).unwrap();
    }

    // Test - verify feature
    fn test_write_hello_world(ctx: &Context) {
        // may panic
        let mut file = ctx.file.try_clone().unwrap();

        // may panic
        file.write_all(b"Hello, world!\n").unwrap();

        // demonstrating assertion
        assert_eq!(1,1);

        std::mem::drop(file);
    }

    // Test - verify feature
    fn test_write_hello_europe(ctx: &Context) {
        // may panic
        let mut file = ctx.file.try_clone().unwrap();

        // may panic
        file.write_all(b"Hello, Europe!\n").unwrap();

        // demonstrating assertion
        assert_eq!(1,1);

        std::mem::drop(file);
    }

    // Defining Utest formed by setup, test and teardown
    utest!(hello_world,
        || setup("/tmp/hello_world.txt"),
        |ctx_ref| test_write_hello_world(ctx_ref),
        |ctx|teardown(ctx));


    // Defining Utest formed by setup, test and teardown
    utest!(hello_europe,
        || setup("/tmp/hello_europe.txt"),
        test_write_hello_europe,
        teardown);
}

Executing the example code cargo test -p test-generator-example testsuite the testsuote above will print the following output.

running 2 tests
test testsuite::hello_europe ... ok
test testsuite::hello_world ... ok

test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 3 filtered out

Please let me know about possibel improvements via github-issues at test-generator-utest