Skip to content

Instantly share code, notes, and snippets.

View CosmicSyntax's full-sized avatar

Daniel Choi CosmicSyntax

View GitHub Profile
@CosmicSyntax
CosmicSyntax / main.rs
Last active July 6, 2020 04:46
Atomic Primatives and Memory Synchronization
use std::sync::atomic::{AtomicUsize, Ordering};
// An atomic primative defined in the global scope
static GLOBAL_DATA: AtomicUsize = AtomicUsize::new(0);
fn main() {
// Atomic primative changed
let old_global_data = GLOBAL_DATA.fetch_add(1, Ordering::SeqCst);
// Any access to this data afer stay after, before stay before
@CosmicSyntax
CosmicSyntax / main.rs
Created May 22, 2020 04:34
Memory Size and Alignment
// Packing the data structure memory alignment to 1 byte
#[repr(packed(1))]
struct Packed {
one: u8, // 1 byte
two: u32, // 4 bytes
three: u16, // 2 bytes
}
struct NotPacked {
one: u8, // 2 bytes (with padding)
@CosmicSyntax
CosmicSyntax / main.rs
Created May 23, 2020 01:01
Const Generics
#![feature(const_generics)]
#[derive(Copy, Clone)]
struct Go<const SIZE: usize> {
array: [Option<u8>; SIZE],
}
fn main() {
let z = Go {
@CosmicSyntax
CosmicSyntax / lib.rs
Last active May 30, 2020 01:46
Implementing a trait only for types satisfying a const-generic predicate! (Nightly Only)
#![allow(incomplete_features)]
#![feature(const_generics)]
pub use core::mem::*;
// Example:
/// `Unaligned` is implemented for all types `T` where `align_of::<T>() == 1`
///
/// `Unaligned` is implemented for `Foo`:
///
@CosmicSyntax
CosmicSyntax / main.rs
Created May 31, 2020 06:15
Trait, with associated type, used as a parameter in a function
trait Test {
type Go;
fn ret(&self) -> Option<Self::Go>;
}
fn wrap(t: impl Test<Go=i32>) {
t;
}
@CosmicSyntax
CosmicSyntax / lib.rs
Last active June 7, 2020 15:32
Lifetime Subtyping (Optional)
struct Context<'s>(&'s str);
struct Parser<'c, 's: 'c> {
// 's will live at least as long as 'c
// Current compiler is not smart enough where subtyping is not needed
context: &'c Context<'s>,
}
impl<'c, 's> Parser<'c, 's> {
fn parse<'d>(&'d self) -> Result<(), &'s str> {
@CosmicSyntax
CosmicSyntax / main.rs
Created June 12, 2020 18:28
Creating uninitialized variables to reduce memcopy
#![feature(new_uninit)]
// On Nightly
struct Hi {
one: u8,
}
fn main() {
// uninit
@CosmicSyntax
CosmicSyntax / main.rs
Created June 24, 2020 02:29
Pinned Self Referencial Struct on Stack
use std::pin::Pin;
use std::fmt;
struct Type {
val: u8,
me: *const Self,
}
fn main() {
@CosmicSyntax
CosmicSyntax / main.rs
Created October 13, 2020 04:18
Bound by lifetime 'a vs. having a lifetime 'a
#![allow(dead_code)]
#![allow(unused_variables)]
// only takes ref types bounded by 'a
// these two example are the same thing from what I understand
fn t_ref<'a, T>(t: &'a T) {}
fn t_ref2<'a, T: 'a>(t: &T) {}
fn t_stat<'a, T: 'a>(t: T) {}
fn t_stat2<T: 'static>(t: T) {}
@CosmicSyntax
CosmicSyntax / main.rs
Created January 28, 2021 04:37
Higher-Rank Trait Bounds
struct Closure<F> {
data: (u8, u16),
func: F,
}
impl<F> Closure<F>
where
for<'z> F: Fn(&'z (u8, u16)) -> &'z u8,
{
fn call<'a>(&'a self) -> &'a u8 {