This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![feature(const_generics)] | |
#[derive(Copy, Clone)] | |
struct Go<const SIZE: usize> { | |
array: [Option<u8>; SIZE], | |
} | |
fn main() { | |
let z = Go { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![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`: | |
/// |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait Test { | |
type Go; | |
fn ret(&self) -> Option<Self::Go>; | |
} | |
fn wrap(t: impl Test<Go=i32>) { | |
t; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![feature(new_uninit)] | |
// On Nightly | |
struct Hi { | |
one: u8, | |
} | |
fn main() { | |
// uninit |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::pin::Pin; | |
use std::fmt; | |
struct Type { | |
val: u8, | |
me: *const Self, | |
} | |
fn main() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![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) {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { |
OlderNewer