Skip to content

Instantly share code, notes, and snippets.

@krdln
krdln / abx.rs
Last active October 10, 2022 20:46
Microbenchmarks for niche get discriminant codegen
#![crate_type="lib"]
pub enum Bool {
True,
Other(char), // try reordering variants
False,
}
use Bool::*;
@krdln
krdln / ctors.md
Last active September 29, 2021 11:43
Everything you need to know about constructors

krdln's blog

Everything you need to know about constructors

2022-05-03, tags: C++, notes

Default arguments are awesome!

Don't try to avoid them just because some other langs cough Rust cough don't have them!

@krdln
krdln / map_2d_iter_mut.rs
Created June 30, 2016 16:53 — forked from Cobrand/map_2d_iter_mut.rs
Trouble for creating an iterator over a 2d map : example file
#[allow(dead_code)]
// BASIC STRUCTURES
// ================
pub type Result<T> = ::std::result::Result<T,Error>;
#[derive(Debug,Copy,Clone,Eq,PartialEq)]
pub enum Reason {
NegativeMapLength,
OutOfRange,
UnknownReason
#!/bin/bash
# rustdatehash: computes a git commit hash for a nightly from a given date.
# Works from about 2015-03-01
# usage: ./rustdatehash yyyy-mm-dd
set -e
set -u
set -o pipefail
@krdln
krdln / playground.rs
Last active August 29, 2015 14:25 — forked from anonymous/playground.rs
Shared via Rust Playground
...
macro_rules! nada {
() => { unsafe { std::mem::uninitialized() } }
}
#[derive(Debug)]
struct Konnection {
hp : String,
use std::num::Wrapping;
enum Instr {
Inc(Wrapping<u8>),
Move(isize),
Loop(Box<[Instr]>),
Print
}
use Instr::*;
@krdln
krdln / ecs.rs
Last active August 29, 2015 14:22
ECS with tuple trait magic
use std::collections::BTreeMap;
extern crate typemap;
use typemap::{TypeMap, Key};
/// Keys for my ECS
trait MyKey: Key where <Self as Key>::Value: Clone {}
struct Ecs {
// just an example layout. totally not cache-friendly
@krdln
krdln / expanded.rs
Last active August 29, 2015 14:11
Macro for entity manager (only main.rs is updated)
fn main() {
{
let em = entity_manager;
em.entities().filter_map(|entity|
Some(entity)
.and_then(|tuple|
entity_manager
.get_component::<X>(&tuple.0)
.map(|obj|tuple.tup_append(obj))
)
@krdln
krdln / send.md
Last active August 29, 2015 14:06
Fork-join concurrency and Send bound

In fork-join concurrency scenario (here's an example of what the interface may look like) it may happen that we would want to pass Arc<Mutex<Vec<&mut int>>> to child threads (you can imagine that instead of int there's really big structure that parent thread could access only by reference, so I think it's a pretty reasonable scenario).

So we want to wrap Vec<&mut int> in a Mutex, but that requires Send and our vector doesn't fulfill Send. So to be able to do that, we may just remove Send bound from Mutex::new. We may think that it won't introduce any unsafety, because Send or 'static bound will be checked by spawn or similar methods anyway.

But unfortunately, now we are able to pass Arc<Mutex<Rc>> too, which for sure shouldn't be legal, because of internal, non-thread safe, mutability of Rc.

So my proposed solution is to keep Send bound for Mutex::new, but decouple Send from 'static. Send would only mean "has Sync env

@krdln
krdln / fork-join.rs
Last active August 29, 2015 14:06
Simple fork-join concurrency in Rust.
#![feature(unboxed_closures, unboxed_closure_sugar)]
use std::rt::thread::Thread;
use std::kinds::marker::InvariantType;
use std::mem::transmute;
fn main() {
let mut arr = [0, 1, 2, 3, 4];
let _ = { // change _ to _bg and it fails to compile!
let mut handle = arr.mut_iter(); // ref|:| ICEd rustc