Skip to content

Instantly share code, notes, and snippets.

pub enum TileHolder<T> { // <<<< doesn't work
EmptyTile(T),
DataTile(~Array8x8<T>)
}
//...
impl<T:Clone+Eq> TileHolder<T> {
fn change_to_data_if_not(&mut self,desiredValue:&T) {
match self {
&EmptyTile(ref x)=> {
if x!=desiredValue
@dobkeratops
dobkeratops / gist:5947345
Created July 8, 2013 09:05
matrix implementatino
pub use self::vecmath::*;
mod vecmath;
#[deriving(Clone,ToStr)]
pub struct Matrix4<T> {
ax:T,ay:T,az:T,pos:T
}
#[deriving(Clone,ToStr)]
pub struct Matrix3<T> {
#[allow(default_methods)];
// TODO - parameterize the type f32 ..
// eg Vec3<T> etc..
//
#[deriving(Clone,ToStr)]
pub use std::num::*;
pub type VScalar=f32;
pub struct Vec2 {x:VScalar, y:VScalar}
@dobkeratops
dobkeratops / gist:5947381
Created July 8, 2013 09:13
2d arrays, fixed size arrays (some 3d )
#[allow(default_methods)];
pub use std::num::*;
pub use std::vec::*;
pub type Index=uint;
pub type Index2d=(Index,Index);
pub type Index3d=(Index,Index,Index);
/*
Fixed size arrays
*/
pub use self::array2d::*;
mod array2d;
/*
Tiled sparse 2d array - pointers to tile contents
eg suitable for image layers
*/
// todo - in C++ _2dTileSize would be template param
// find workarounds for rust
@dobkeratops
dobkeratops / cgbase.rs
Last active December 24, 2015 19:48
ideas for a base module for a common vector maths library. NOT a complete vector maths library itself, just traits/structs that are easy to map *other* libraries to for easy interoperability . implementations of vector maths may vary depending on the authors own context. the suggestion is that if rust libextra gains a common vector maths library…
/*!
Traits for exposing coordinate data in different representations
the goal is to allow interoperabiltiy of *data structures* between different libraries
which may differ in internal details whilst manipulating similar same types.
leverage rusts' ability to decouple data from code
--
This is NOT a vector maths library, just common structures & accessors for x[,y[,z[,w]]]
it could be modules at the base of a standard vector maths library
the more potential discussion & divergance there is, the less something should be in this module.
--
use std::io::println;
use rf_common::*;
use syntax::ast;
use rustc::middle::{ty,typeck};
use syntax::codemap::{BytePos, Pos};
use rsfind::ShowDefMode;
...
struct Blob<HEADER> {
data:~[u8],
}
impl<T> std::ops::Deref<T> for Blob<T> {
fn deref<'s>(&'s self)->&'s T {
//assert!(self.num_bytes() > std::intrinsics::size_of::<T>());
unsafe { &*(&self.data[0] as *u8 as *T)
template<typename T, typename OFFSET=int,
int ALIGN_SHIFT=2>
class OffsetPtr
{
OFFSET ofs;
public:
T* operator->() {
return (T*) (((((size_t)this)>>ALIGN_SHIFT)+ofs)<<ALIGN_SHIFT);
};
@dobkeratops
dobkeratops / gist:9716302
Last active August 29, 2015 13:57
Helpers for unsafe code, especially for interfacing with C libaries
// Developped from a use case: use of a precompiled object graph in a single allocation,
// internally using compact relative pointers, interfacing with a C library (openGL)
/// return a reference to a different type at a byte offset from the given base object reference
unsafe fn byte_ofs_ref<'a,X,Y,I:Int=int>(base:&'a X, ofs:I)->&'a Y {
&*( (base as *_ as *u8).offset( ofs.to_int().unwrap() ) as *Y)
}
/// return a raw ptr to a different type at a byte offset from the given base object reference
unsafe fn byte_ofs_ptr<'a,X,Y,I:Int=int>(base:&'a X, ofs:I)->*Y {