2022-05-03, tags: C++, notes
Don't try to avoid them just because some other langs cough Rust cough don't have them!
#![crate_type="lib"] | |
pub enum Bool { | |
True, | |
Other(char), // try reordering variants | |
False, | |
} | |
use Bool::*; |
#[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 |
... | |
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]>), | |
} | |
use Instr::*; |
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 |
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)) | |
) |
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
#![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 |