Skip to content

Instantly share code, notes, and snippets.

View allwelldotdev's full-sized avatar

Allwell allwelldotdev

View GitHub Profile
@allwelldotdev
allwelldotdev / niche_optimization.rs
Created October 20, 2025 11:46 — forked from rust-play/playground.rs
Code shared from the Rust Playground
// Niche optimization
// A clever way to make Option<T> (or similar enums) use the exact same memory
// size as T itself, without wasting space for a "tag"
// (like a boolean for Some/None).
use std::mem::{size_of, MaybeUninit};
fn main() {
// Seeing the size overhead of using Option types
println!("size_of::<i32>(): {}", size_of::<i32>());
@allwelldotdev
allwelldotdev / heapless_vector_type3.rs
Last active October 20, 2025 11:20 — forked from rust-play/playground.rs
Code shared from the Rust Playground
// Creating a heapless vector type using MaybeUninit<T> safely with
// unintialized memory (useful in embedded systems where there's limited
// memory space for heap allocations).
#![no_std]
extern crate std;
use arrayvec::ArrayVec;
mod arrayvec {
@allwelldotdev
allwelldotdev / heapless_vector_type2.rs
Last active October 20, 2025 11:24 — forked from rust-play/playground.rs
Code shared from the Rust Playground
// Creating a heapless vector type using MaybeUninit<T> safely for
// unintialized memory (useful in embedded systems where there's limited
// memory space for heap allocations).
#![no_std]
extern crate std;
use arrayvec::ArrayVec;
mod arrayvec {
@allwelldotdev
allwelldotdev / heapless_vector_type.rs
Last active October 20, 2025 11:22 — forked from rust-play/playground.rs
Code shared from the Rust Playground
// Creating a heapless vector type (useful in embedded systems where there's
// limited memory space for heap allocations)
#[derive(Debug)]
struct ArrayVec<T, const N: usize>
where
T: Copy
{
values: [Option<T>; N],
len: usize,
@allwelldotdev
allwelldotdev / atomics_mem_ordering_acquire_release.rs
Last active October 14, 2025 09:11 — forked from rust-play/playground.rs
Code shared from the Rust Playground
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::thread;
static DATA: AtomicU64 = AtomicU64::new(0); // Atomic Data
static READY: AtomicBool = AtomicBool::new(false); // Flag
fn main() {
let _producer = thread::spawn(|| {
// ...Expensive operations captured by Ordering::Release.
DATA.store(123456, Ordering::Relaxed); // Data store here, though relaxed, is covered by Ordering::Release.