Skip to content

Instantly share code, notes, and snippets.

use std::io::println;
use rf_common::*;
use syntax::ast;
use rustc::middle::{ty,typeck};
use syntax::codemap::{BytePos, Pos};
use rsfind::ShowDefMode;
...
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);
};
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)
@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 {
@dobkeratops
dobkeratops / gist:9805755
Created March 27, 2014 11:46
test of creating slices within immutable blob, safety asserted on creation.
use std::{io,mem,raw,cast};
struct BlobContainingSlices<'a> {
data:~[u8],
subset1: &'a [u32],
subset2: &'a [u16],
}
fn slice_within_as<TO,FROM>(owner:&[FROM], start:uint, len_in_new_units:uint)->&[TO] {
@dobkeratops
dobkeratops / gist:9835336
Last active August 29, 2015 13:57
internal vtables from rust traits
#[feature(macro_rules)];
use std::{cast};
// emulating C++ classes using Traits as vtables, could the safety be improved?
pub trait Foo {
fn foo(&self,i:int);
}
pub struct CppClass<RawDataStruct,TraitInterface> {
@dobkeratops
dobkeratops / gist:9841737
Last active August 29, 2015 13:57
internal vtable / c++ class emulation , again
#[feature(macro_rules)];
#[allow(macro_rules)];
use std::{io,mem,raw,cast};
// emulating C++ classes using Traits as vtables, could the safety be improved?
pub trait Foo {
fn foo(&self,i:int);
}
struct A{
x:int,y:int
}
struct B{
p:int,q:int
}
struct C{
a:A,b:B
}
@dobkeratops
dobkeratops / scattergather.rs
Created April 27, 2014 22:39
Scatter Gather Iterators
#![feature(default_type_params)]
use std::iter::RandomAccessIterator;
trait ScatterInto<'a, T,INDEX=uint> {
fn scatter_into<SRC:Iterator<(INDEX,T)>> (&mut self, src:SRC);
fn scatter_by_indices<IDXS:Iterator<INDEX>, TS:Iterator<T>> (&mut self, indices:IDXS,values:TS) { self.scatter_into(indices.zip(values) ) }
}
trait ScatterIntoGrow<'a, T,INDEX=uint> {
/// Scatter values into a collection, and grow it if its not large enough
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