0

I am trying to return an array of tuples in Rust, but I am running into issues with lifetimes:

pub trait Vertex{
    fn map_components<'a>() -> &'a[(GLint, GLenum)];
}

pub struct VertexPos2d {
    // Position
    x: GLfloat,
    y: GLfloat
}

impl Vertex for VertexPos2d{
    fn map_components<'a>() -> &'a[(GLint, GLenum)]{
        return &[
            (2, gl::FLOAT) as (GLint, GLenum)
        ];
    }
}

If I remove the tuple from the array everything seems to work fine. I've tried to adjust the lifetimes such that the tuples in the array have the same lifetime as the array itself but have not been able to get it to work. Are there some changes I can make to get this to work?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
rykeeboy
  • 645
  • 2
  • 8
  • 22
  • You aren't returning an array, you are returning a **reference** to an array. Also, you are attempting to return an array coerced to a *slice*, not an array directly. – Shepmaster Aug 23 '16 at 23:13
  • 1
    You probably don't want to return a reference.. and instead just return an array. The problem you no doubt faced was that the slice of tuples has no `Sized` marker (or a similar error). This can be fixed in two ways.. firstly, by sizing the array: `-> [(GLint, GLenum); 1]` or by boxing it up. Here is my attempt at showing you how that might play out: https://play.rust-lang.org/?gist=92bf0eedc0f66d51499767a0d73d271d&version=stable&backtrace=0 – Simon Whitehead Aug 23 '16 at 23:29
  • `-> [(GLint, GLenum); 1]` is how you'd return an array, so that's the most likely solution. You *may* also be interested in returning a `Vec<(GLint, GLenum)>`. arrays / slices / vectors / boxed slices all offer different capabilities and tradeoffs. – Shepmaster Aug 23 '16 at 23:45
  • I can't restrict the number of items that are returned in the array in my case. I'll take a look at using a vector instead. This makses sense to me – rykeeboy Aug 23 '16 at 23:48

0 Answers0