Skip to content

Instantly share code, notes, and snippets.

View KevinWMatthews's full-sized avatar
🦀
Studying Rust

Kevin W Matthews KevinWMatthews

🦀
Studying Rust
View GitHub Profile
@KevinWMatthews
KevinWMatthews / machine.js
Created December 4, 2019 13:40
Generated by XState Viz: https://xstate.js.org/viz
const machine = Machine({
id: 'sensor',
initial: 'inactive',
states: {
inactive: {
on: {
HW_HIGH: 'active'
}
},
active: {
@KevinWMatthews
KevinWMatthews / machine.js
Last active December 4, 2019 12:37
Generated by XState Viz: https://xstate.js.org/viz
const level_active_high = {
initial: 'inactive',
states: {
inactive: {
on: {
HW_HIGH: 'active'
}
},
active: {
@KevinWMatthews
KevinWMatthews / machine.js
Created December 1, 2019 19:00
Generated by XState Viz: https://xstate.js.org/viz
const initialState = 'inactive';
const toggleMachine = Machine({
id: 'toggle',
initial: initialState,
states: {
inactive: { // 'inactive' state
on: { // "on event, transition to state..."
TOGGLE: 'active' // on event 'TOGGLE', transition to 'active'
}
@KevinWMatthews
KevinWMatthews / machine.js
Last active December 1, 2019 17:01
Generated by XState Viz: https://xstate.js.org/viz
const initialState = 'inactive';
const toggleMachine = Machine({
id: 'toggle',
initial: initialState,
states: {
inactive: { // 'inactive' state
on: { // "on event, transition to state..."
TOGGLE: 'active' // on event 'TOGGLE', transition to 'active'
}
@KevinWMatthews
KevinWMatthews / rust-string-concat.rs
Last active September 16, 2019 14:48
String concatenation do's and dont's in Rust
// Uncomment /* */ blocks within functions to see compiler errors.
/*
Links to documentation
str, the string primitive:
https://doc.rust-lang.org/std/primitive.str.html
str, trait implementations:
https://doc.rust-lang.org/std/primitive.str.html#implementations
@KevinWMatthews
KevinWMatthews / reduce_demo.py
Created August 11, 2019 20:17
Reduce in Python
# Docs:
# https://docs.python.org/3/library/functools.html#functools.reduce
import functools
# reduce(function, iterable)
# Applies function to each item of the sequence, left to right.
# Passes two arguments to the function:
# the current total
# the next item in the sequence
@KevinWMatthews
KevinWMatthews / mutable_reference_scope.rs
Created June 28, 2019 16:31
Rust borrowing rules - reference scope and non-lexical lifetimes
// Example of non-lexical lifetimes for references in Rust
//
// References go out of scope at their last use,
// not at the end of a block.
fn main() {
example1();
example2();
example3();
}
@KevinWMatthews
KevinWMatthews / exercism-cpp-CMakeLists.txt
Last active August 30, 2018 16:23
CMakeLists.txt from Exercism's C++ track
# Get the exercise name from the current directory
get_filename_component(exercise ${CMAKE_CURRENT_SOURCE_DIR} NAME)
# Basic CMake project
cmake_minimum_required(VERSION 3.1.3)
# Name the project after the exercise
project(${exercise} CXX)
# Locate Boost libraries: unit_test_framework, date_time and regex
@KevinWMatthews
KevinWMatthews / cmac_aes_mock.c
Last active March 24, 2018 05:26
Overview and example of mocking an AES encryption library
// Bird's-eye view of overall production code
Cmac_Calculate()
{
aes_handle = Aes128_Create(key, iv)
// Perform several technical operations (CmacOps) per the CMAC spec.
CmacOps_GenerateSubkeys(aes_handle, *subkey1, *subkey2)
//...
CmacOps_ApplyCbcMac(aes_handle, <relevant_parameters>)
//...
// From production code
typedef struct AES_STRUCT * AES_HANDLE;
typedef struct
{
char *key;
size_t key_len;
char *iv;
size_t iv_len
} CREATE_PARAMS;