rust-lang / rust-lang/rfcs

It's not possible to get the size of the offset from an arbitrary structure to it's field.

Open
#710 6 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

T-lang T-libs
Dominant language
Markdown
Stars
6.6k
Forks
1.7k
Avg merge
16h 14m
Merged PRs (30d)
1

Description

Issue by sstewartgallus
Wednesday Jul 31, 2013 at 04:38 GMT

For earlier discussion, see https://github.com/rust-lang/rust/issues/8144

This issue was labelled with: in the Rust repository


I have the following code at http://gitorious.org/p-x/p-x/blobs/master/src/gl.rs#line465 but it is very ugly. I have to create a data structure so I can calculate the offset from the structure to it's field, and pass it to the low level OpenGL code. To understand the issue better the documentation at http://www.khronos.org/opengles/sdk/docs/man/xhtml/glVertexAttribPointer.xml for the glVertexAttribPointer function may be useful.

impl <'self, S: Zero, T: AttributeValue> DisabledAttribute<'self, T> {
    pub fn vertex_pointer<C: Component<S, T>, State>(
        &self,
        _: &BufferTarget<'self, State, S>,    // Not used just needs to be bound
        normalized: bool,
        component: &C
    ) {
        let handle = self.raw_attribute;

        // Compute the stride, and offset
        let s = &Zero::zero();
        let t = component.access(s);
        let offset = borrow::to_uint(t) - borrow::to_uint(s);

        do self.linked_program.raw_program.context.send_and_recv {
            AttributeValue::vertex_attrib_pointer::<T>(
                handle,
                normalized,
                sys::size_of::<S>(),
                offset
            )
        }
    }
}

The definition of Component:

pub trait Component<S, T> {
    fn access<'r>(&self, &'r S) -> &'r T;
}

A sample implementation:

struct Positions;
impl Component<assets::Vertex, [f32, ..3]> for Positions {
    fn access<'r>(&self, s: &'r assets::Vertex) -> &'r [f32, ..3] {
        &s.position
    }
}

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the earlier discussion in Rust issue #8144 and the example in src/gl.rs around line 465. Clarify the intended language or library API for obtaining a field offset from an arbitrary structure, including its safety and type constraints; the work is done when the desired behavior and acceptance criteria are defined well enough to replace the example's pointer-arithmetic approach.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.