Skip to content

Instantly share code, notes, and snippets.

View wdevore's full-sized avatar
💭
I am currently working on the next phase of my neuron simulator in Golang

William Cleveland wdevore

💭
I am currently working on the next phase of my neuron simulator in Golang
View GitHub Profile
@wdevore
wdevore / cell.rs
Created February 10, 2019 19:30
An example of Interior mutability for Ranger engine
use std::cell::{Cell, RefCell};
use std::rc::Rc;
type RefNode = Rc<NodeTrait>;
trait NodeTrait {
fn id(&self) -> usize {
0
}
fn set_id(&self, id: usize);
@wdevore
wdevore / refcell_example.rs
Created February 10, 2019 19:32
Another example of returning an option<> with using take()
use std::cell::RefCell;
use std::rc::Rc;
trait NodeTrait {
fn get_x(&self) -> f32;
fn add(&mut self, v: f32);
fn step(&mut self);
fn process(&mut self);
fn replacement(&self) -> &Option<RNode> {
&None
@wdevore
wdevore / reference_example.rs
Created February 10, 2019 19:35
An example of lifetimes and returning a SceneManager with correct lifetime attribute
// See this users response
// https://users.rust-lang.org/t/typical-cant-borrow-issue/24817
trait NodeTrait {
fn get_x(&self) -> f32;
fn add(&mut self, v: f32);
fn step(&mut self);
fn process(&mut self);
fn print(&self) {
println!("node: {}", self.get_x());
@wdevore
wdevore / refcell_interior_mut.rs
Created February 10, 2019 19:37
An example of using RefCells to handle interior mutability
use std::cell::RefCell;
use std::rc::Rc;
type RefNode = Rc<RefCell<NodeTrait>>;
type RefTransform = Rc<RefCell<TransformProperty>>;
trait NodeTrait {
fn id(&self) -> usize {
0
}
@wdevore
wdevore / mutable_mut_reference.rs
Created February 11, 2019 18:48
An example of passing a mutable borrow that is mutable parameter
// https://www.snoyman.com/blog/2018/11/rust-crash-course-05-rule-of-three
#[derive(Debug)]
struct Person {
name: String,
age: u32,
}
fn birthday_immutable(person: &mut Person) {
person.age += 1;
@wdevore
wdevore / extract_option_cell.rs
Created March 10, 2019 17:43
Howto extract Option from Cell of type usize
// How to extract Option from Cell
pub struct X {
replacement: Cell<Option<usize>>,
...
}
fn get_transition_node(&self) -> usize {
if let Some(rep) = &*self.replacement.borrow() {
let repl = rep.borrow();
return repl.data().borrow().node.id();
@wdevore
wdevore / top.v
Created September 4, 2019 01:42
Verilog 4bit up-counter using Icestorm framework and targeting the tinyFPGA-B2 board
// 4bit up counter - top.v
// This is expects a pcf specific to the tinyFPGA-B2 board
module top (
pin1_usb_dp, // USB pull-up enable, set low to disable
pin2_usb_dn,
pin3_clk_16mhz, // 16 MHz on-board clock
pin13, // Reset, active high, synchonous reset input
pin12, // Enable, active high on counter
pin11, // MSB output pin
@wdevore
wdevore / top.v
Created September 4, 2019 02:12
Basic shift register in Verilog that targets a tinyFPGA-B2 pcf. Note: this is just for learning, don't do it this way.
// shift_register - top.v
//
// Description:
// shifts bits to left
// Pins are defined in pins.pcf file.
//
// out <-- pin13, pin12, pin11, pin10...,pin6 <-- pin5 in
module top (
@wdevore
wdevore / top.v
Last active September 7, 2019 21:49
Verilog cylon using Icestorm framework and targeting the tinyFPGA-B2 board
// cylon effect - top.v
//
// pins are relative to the tinyFPGA-B2
// I really need to stop using reg bits and use a vector. <-- lazy person.
module top (
output pin1_usb_dp,// USB pull-up enable, set low to disable
output pin2_usb_dn,
input pin3_clk_16mhz, // 16 MHz on-board clock
output pin13,
@wdevore
wdevore / cylon.v
Created September 7, 2019 23:41
Verilog cylon using Icestorm framework and targeting the tinyFPGA-B2 board. Properly uses a register and the shift operator.
// cylon effect - top.v
//
// pins are relative to the tinyFPGA-B2
module top (
output pin1_usb_dp,// USB pull-up enable, set low to disable
output pin2_usb_dn,
input pin3_clk_16mhz, // 16 MHz on-board clock
output pin13,
output pin12,