Skip to content

Instantly share code, notes, and snippets.

View zesterer's full-sized avatar

Joshua Barretto zesterer

View GitHub Profile
use std::sync::mpsc;
use std::sync::{Arc, Mutex};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::collections::HashMap;
use std::thread;
trait Serialize {}
trait Deserialize {}
impl Serialize for String {}
impl Deserialize for String {}
fn start_networking (remote: &str) {
let mut threadpool = ThreadPool::new(10);
let mut postoffice = PostOffice::new(remote);
while let Ok(req) = postoffice.await_new() {
threadpool.exec(move || {
match req {
Req::Chunk(postbox) => handle_chunk_req(postbox),
Req::Entity(postbox) => handle_entity_req(postbox),
Req::Inventory(postbox) => handle_inventory_req(postbox),
@zesterer
zesterer / widgets.rs
Last active August 7, 2018 12:26
Example Rust widget library design
use std::cell::RefCell;
use std::rc::Rc;
trait Widget {
fn add(&self, w: Rc<Widget>) -> Rc<Widget> { w }
fn children(&self) -> Vec<Rc<Widget>> { vec!() }
fn type_name(&self) -> &'static str;
fn print_tree(&self, depth: i32) {
for _ in 0..depth { print!(" "); }
// Server-side
impl Server {
fn start(&self) {
let listener = TcpListener::bind("0.0.0.0:59003").unwrap();
// Listen for new clients connecting
for stream in listener.incoming() {
// When a client connects, generate a UID for them...
let client_uid = self.generate_uid();
// ...spawn a new job to handle all of their communication...
@zesterer
zesterer / abstractions.txt
Last active August 15, 2018 09:21
A quote by me
If every problem in Computer Science can be solved by either
adding or removing a layer of abstraction, there's only one
reasonable conclusion to come to:
You're using the wrong abstraction.
@zesterer
zesterer / trait.rs
Last active August 15, 2018 10:42
Return a borrowed trait object by reference from a method in Rust
trait Volume {}
struct RawChunk; impl Volume for RawChunk {}
struct RleChunk; impl Volume for RleChunk {}
struct FileChunk; impl Volume for FileChunk {}
struct Container {
raw: Option<RawChunk>,
rle: Option<RleChunk>,
file: Option<FileChunk>,
@zesterer
zesterer / veloren-principles.md
Last active August 15, 2018 15:37
Some useful development principles

Principles Of Development

Below are a series of principles you should try to keep to when developing code for Veloren

Criticism is how you learn: get used to it!

Don't get attached to your code. Expect that everything you write will be poked, prodded, rewritten, refactored, deleted or used in a context you don't envisage. Don't take criticism of your code personally, and realise that all programmers are worthy of criticism at times.

Code is written once and read 100 times!

#![feature(integer_atomics)]
use std::sync::{mpsc, Arc, Mutex, atomic::{AtomicU64, Ordering}};
use std::collections::HashMap;
use std::thread;
use std::marker::PhantomData;
// Msg
trait Msg: Send + Sync + 'static {}
@zesterer
zesterer / manager_pattern.rs
Created August 22, 2018 10:14
A pattern for correctly managing worker threads associated with an instance in Rust
struct Server;
impl Server {
fn new() -> Server {
Server
}
}
struct ServerMgr {
server: Arc<Server>,
@zesterer
zesterer / typed_noise.rs
Last active August 26, 2018 01:04
Typed noise implementation
use std::ops::{Add, Mul};
use std::marker::PhantomData;
// Generic NoiseFn type
trait NoiseFn: Copy + Clone {
type Inp: Copy + Clone;
type Out: Copy + Clone;
fn get(&self, i: Self::Inp) -> Self::Out;