Skip to content

Instantly share code, notes, and snippets.

@Zoxc
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Zoxc/2a69723a14704db3d50f to your computer and use it in GitHub Desktop.
Save Zoxc/2a69723a14704db3d50f to your computer and use it in GitHub Desktop.
Field offsets RFC
  • Start Date: 2014-07-21
  • RFC PR #: (leave this empty)
  • Rust Issue #: (leave this empty)

Summary

This adds the ability to refer to fields of types and use them later on with objects of that type. The FieldOffset<Obj, Field> type is added which refers to fields of type Field in the object Obj. The offsetof Type.field syntax is defined to construct instances of FieldOffset<Type, the type of field>.

Motivation

Linked lists with internal storage.

struct Object {
    ListData list_a;
    ListData list_b;
}

fn main() {
    let obj = Object { list_a: ListData::new(), list_b: ListData::new() }

    let mut a = List::new(offsetof Object.list_a);
    let mut b = List::new(offsetof Object.list_b);
    
    a.add(&obj);
    b.add(&obj); 
    
    // obj is now a member of both lists and no heap allocation have taken place
}

Detailed design

Suggested interface:

struct FieldOffset<Obj, Field>;

impl FieldOffset<Obj, Field> {
    pub fn get(obj: &T) -> &FieldType;
    pub fn get_mut(obj: &mut T) -> &mut FieldType;
    pub fn get_raw(obj: *const T) -> *const FieldType;
    pub fn get_raw_mut(obj: *mut T) -> *mut FieldType;
}

Example use:

struct Test {
    field: int
}

fn main() {
    let off: FieldOffset<Test, int> = offsetof Test.field;

    let mut t = Test { field: 1 }

    *off.get_mut(&t) = 2;

    println!("field is {}", t.field);
}

Drawbacks

One more builtin type and offsetof won't be usable for something weird.

Alternatives

An alternative is to implement this using unsafe code.

However if values in type parameters is going to be allowed it is desirable to pass it as type parameters, but that can't work with unsafe code. In the list example above you could specialize the list type for a specific field, like List<offsetof Object.list_a>. This generates more efficient code since the list won't have to look up the field offset at runtime. An alternative to that again is to do something like this using traits:

trait FieldOffset<Obj, Field> {
    pub fn get(obj: &Obj) -> &Field;
}

struct Object {
  ListData list_a;
  ListData list_b;
}

struct ObjectListA;

impl FieldOffset<ListData> for ObjectListA {
    pub fn get(obj: &Obj) -> &Field {
	&obj.list_a
    }
}

struct ObjectListB;

impl FieldOffset<ListData> for ObjectListB {
    pub fn get(obj: &Obj) -> &Field {
	&obj.list_b
    }
}

struct List<Obj, Field: FieldOffset<Obj, ListData>>;

static list: List<Object, ObjectListA>;

Another alternative is to allow offsetof Test.field to return an uint which could be passed as a type parameter. However that requires the compiler to know the layout of fields at the type checking stage.

Unresolved questions

None.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment