Skip to content

Instantly share code, notes, and snippets.

@yongkyuns
yongkyuns / binary_tree.rs
Created December 30, 2020 14:14
Binary tree with indexing
// Binary tree with index, copied from https://sachanganesh.com/programming/graph-tree-traversals-in-rust/
pub type Idx = usize;
pub struct TreeNode {
pub value: usize,
pub left: Option<Idx>,
pub right: Option<Idx>,
}
@yongkyuns
yongkyuns / parent_child.rs
Created December 30, 2020 01:42
Modelling child that holds partial reference to parent's data using Rc,RefCell
use std::cell::RefCell;
use std::rc::Rc;
use std::rc::Weak;
pub type RefPoint = Rc<RefCell<Point>>;
pub type WeakPoint = Weak<RefCell<Point>>;
#[derive(Debug)]
pub struct Point {
x: f32,
@yongkyuns
yongkyuns / global_arena.rs
Created November 4, 2020 00:43 — forked from AndrewGaspar/global_arena.rs
Global Arena Rust
use std::cell::RefCell;
// `generational_arena` is a special type of arena that allows elements to be recycled, and the
// handles allocated from the arena do not hold a borrow on the arena. The handles are later used to
// retrieve concrete borrows on the arena temporarily. The check for live-ness is essentially
// performed at runtime, rather than being checked by the borrow checker.
use generational_arena::{Arena, Index};
// Allocate `ARENA` at global/thread scope. The arena is wrapped in a RefCell so we can get
// runtime-checked mutable borrows of the arena.
@yongkyuns
yongkyuns / config.fish
Created August 1, 2020 12:36
~/.config/fish/config.fish
set -xg PATH /opt/local/bin /opt/local/sbin $PATH
# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
eval /Users/shinMac/anaconda2/bin/conda "shell.fish" "hook" $argv | source
# <<< conda initialize <<<
" Cursor move to line begin/end in insert mode
inoremap <C-b> <C-o>^
inoremap <C-e> <C-o>$
inoremap <C-h> <Left>
inoremap <C-l> <Right>
inoremap <C-j> <Down>
inoremap <C-k> <Up>
" delete without yanking
// use ease::*;
use ease::{Arrow, EaseType, Tweener};
use nannou::prelude::*;
struct Model {
tweener: Tweener<Arrow>,
}
fn model(app: &App) -> Model {
app.new_window().size(512, 512).view(view).build().unwrap();
//! A suite of common interpolation functions often referred to as "easing" and "tweening"
//! functions. This API is provided by the [pennereq crate](https://docs.rs/pennereq).
pub use crate::geom::{self, pt2, Point2};
use crate::math::BaseFloat;
pub use pennereq::*;
type EaseFn<S> = fn(t: S, b: S, c: S, d: S) -> S;
#[derive(Debug, Copy, Clone)]
@yongkyuns
yongkyuns / drawable.rs
Created July 14, 2020 13:58
Drawable Example
use crate::draw::Draw;
use std::cell::Cell;
use std::cell::RefCell;
use std::rc::Rc;
pub trait Drawable {
fn add_line(&mut self) -> &mut dyn Drawable;
fn shift(&mut self, x: f32, y: f32) -> &mut dyn Drawable;
}
#[derive(Debug, Clone, Copy)]
@yongkyuns
yongkyuns / ffmpeg_example.rs
Created July 7, 2020 23:38
FFMPEG usage in rust using command and raw pixels
use std::io::prelude::*;
use std::process::{Command, Stdio};
fn main() {
let mut child = Command::new("ffmpeg")
// Overwrite file if it already exists
.arg("-y")
// Interpret the information from stdin as "raw video" ...
.arg("-f")
.arg("rawvideo")
@yongkyuns
yongkyuns / default.rs
Created July 3, 2020 16:54
Rust Default
#[derive(Debug)]
struct Point {
x: f32,
y: f32,
}
impl Point {
fn new() -> Self {
Point { x: 30.0, y: 5.0 }
// Default::default() // This also works
}