Skip to content

Instantly share code, notes, and snippets.

View piboistudios's full-sized avatar
💭
Bothering the kind folks of web.libera.chat/#ircv3

Gabriel Hayes piboistudios

💭
Bothering the kind folks of web.libera.chat/#ircv3
View GitHub Profile
@piboistudios
piboistudios / concurrency-getting-started.rs
Last active July 12, 2018 19:07 — forked from rust-play/playground.rs
[RUST]Concurrency 1.0 (#3)
use std::thread;
use std::time::Duration;
// this simple example spwans a thread and runs an operation while running the same operation on the main thread
fn main() {
// the thread::spawn function expects a Closure; Rust closure syntax is |params_list...| { expr body }
// if the exprsesion is one line curly braces can be omitted
// type annotations are optional in closures, as Rust implicitly infers the types the first time the Closure is called
let handle = thread::spawn(|| {
for i in 1..10 {
@piboistudios
piboistudios / parent-children-tree.rs
Last active December 31, 2023 14:15 — forked from rust-play/playground.rs
[RUST]Parent-Children Tree (#2)
// we will use weak references with the Rc<T> (reference counting pointer) type
// weak references allow us to make references to a value that will -not- keep it alive
// this is perfect in the intsance of children, as we will soon see
use std::rc::{Rc,Weak};
use std::cell::RefCell;
// this example builds upon the last by storing a vector of children as well as a parent
#[derive(Debug)]
struct Node {
@piboistudios
piboistudios / cons-list.rs
Last active July 12, 2018 19:06 — forked from rust-play/playground.rs
[RUST]Cons List (#1)
// We need a reference counting pointer to have multiple references to a single object
use std::rc::Rc;
// We need a RefCell to be able to internally mutate an immutable value
use std::cell::RefCell;
#[derive(Debug)]
// definition of a linked list
enum List {
// We may have multiple references to a given list object, therefore we need Rc<T>
@piboistudios
piboistudios / complex-channel.rs
Last active July 12, 2018 19:08 — forked from rust-play/playground.rs
[RUST]Channels (#5)
use std::thread;
use std::sync::mpsc;
use std::time::Duration;
fn main() {
// start by creating a channel
let (tx, rx) = mpsc::channel();
// we need to create a clone of the transmitter because each thread actually
// owns the copy we give it... and it's greedy so it won't give it back :(
@piboistudios
piboistudios / threads-and-channels.rs
Last active July 12, 2018 19:07 — forked from rust-play/playground.rs
[RUST]Concurrency 1.1 (#4)
use std::thread;
use std::time::Duration;
fn main() {
concurrency_demo_1();
concurrency_demo_2();
}
// this simply demonstrates how to move data from an outer scope into a thread's scope:
fn concurrency_demo_1() {
let v = vec![1, 2, 3];
@piboistudios
piboistudios / simple-aabb.rs
Last active July 12, 2018 20:54 — forked from rust-play/playground.rs
Code shared from the Rust Playground
// Simple Axis-Aligned Bounding Box
// Author: Gabriel Hayes
#[derive(Debug)]
struct AABB {
x: i64,
y: i64,
wx: i64,
hy: i64,
}
@piboistudios
piboistudios / cell-types-example.rs
Created July 16, 2018 15:49 — forked from rust-play/playground.rs
Code shared from the Rust Playground
// Practice with Cell Smart Pointers
// Cells grant interior mutability:
// Cells allow you to mutate the interior value, these can only be used with
// values on the stack or Copy values
// RefCells allow you mutate the interior reference use this for values on the heap
// or Clone values
@piboistudios
piboistudios / runloop.rs
Last active July 16, 2018 19:59 — forked from rust-play/playground.rs
Code shared from the Rust Playground
// Author: Gabriel Hayes
// Rust Worker-Runloop Implementation
use std::cell::RefCell;
use std::rc::Rc;
use std::time::Duration;
use std::thread;
struct Worker {
job: Rc<RefCell<FnMut(i64)>>,
}
@piboistudios
piboistudios / shared-objects.rs
Last active July 16, 2018 21:35 — forked from rust-play/playground.rs
Code shared from the Rust Playground
// Author: Gabriel Hayes
use std::cell::RefCell;
use std::rc::{Rc, Weak};
#[derive(Debug)]
struct Node {
val: i64,
children: Vec<Rc<RefCell<Node>>>,
parent: RefCell<Weak<Node>>
}
{
"privateKey": "[PRIVATE_KEY]",
"publicKey": "[PUBLIC_KEY]",
"encryptionKey": "[ENCRYPTION_KEY]"
}