Skip to content

Instantly share code, notes, and snippets.

pub fn load_in_place<'a, T>(bytes: &'a [u8]) -> &'a T {
assert!(!bytes.is_empty());
assert!(validate::<T>(bytes));
unsafe { &*bytes.as_ptr().cast() }
}
#[cfg(test)]
@jsimmons
jsimmons / ref_count.rs
Created April 28, 2022 07:49
Upgradable Reference Counting
use std::{
marker::PhantomData,
ops::Deref,
ptr::NonNull,
sync::atomic::{AtomicI32, Ordering},
};
struct Inner<T: ?Sized> {
// Number of strong references in addition to the current value.
// A negative value indicates a non-atomic reference count, counting up from i32::MIN
@jsimmons
jsimmons / frk.rs
Created January 2, 2022 22:30
Upgradable Atomic Ref Counting
/// Flexible Reference Kounting
use std::{
marker::PhantomData,
ops::Deref,
ptr::NonNull,
sync::atomic::{AtomicI32, Ordering},
};
struct Inner<T: ?Sized> {
// Number of strong references in addition to the current value.
@jsimmons
jsimmons / main.rs
Created April 3, 2021 09:51
Upgradable Rc / Arc
mod rc;
use rc::{BoxArc, BoxRc};
struct Widget {
value: i32,
}
impl Widget {
fn new(value: i32) -> Self {
use std::{
ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign},
time::{Duration, Instant},
};
use sdl2::{event::Event, rect::Rect, render::WindowCanvas, Sdl};
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
pub struct Vec2 {
pub x: i32,
use sdl2::{event::Event, keyboard::Keycode};
use slappy::{Colour, Draw, Input, Loop, TickState, Vec2};
#[derive(Default)]
struct PongInput {
player_1_up: bool,
player_1_down: bool,
player_2_up: bool,
player_2_down: bool,
}
@jsimmons
jsimmons / spinlock.rs
Created January 4, 2020 22:31
Dont use this please
use std::cell::UnsafeCell;
use std::sync::atomic::{spin_loop_hint, AtomicBool, AtomicU32, Ordering};
use std::sync::{Barrier, Mutex};
use std::ops::{Deref, DerefMut};
use std::thread;
use parking_lot;
struct LockInner(AtomicBool);
const LOCKED: bool = true;
#include <elfutils/libdwfl.h>
#define UNW_LOCAL_ONLY
#include <libunwind.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <unistd.h>
#include <errno.h>
@jsimmons
jsimmons / rays.rs
Created August 11, 2019 23:16
Rays on a Plane
use std::fs::File;
use std::io::BufWriter;
use std::io::Write as IoWrite;
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};
use std::path::Path;
use rand::{Rng, SeedableRng};
use rand_distr::{Distribution, UnitDisc, UnitSphere};
use rand_pcg;
let mapleader=","
" Specify a directory for plugins
" - For Neovim: ~/.local/share/nvim/plugged
" - Avoid using standard Vim directory names like 'plugin'
call plug#begin('~/.local/share/nvim/plugged')
Plug 'godlygeek/tabular'
Plug 'rust-lang/rust.vim'
Plug 'ctrlpvim/ctrlp.vim'