rust - How can I specify lifetimes in associated types? -


i'm trying each graphicscontext implementation return different implementation of shader.

pub trait resources {     type shader: shader::shader; }  pub trait graphicscontext {      type resources: resources;      /// creates shader object     fn create_shader<'a>(&'a self, shader::stage, source: &str)         -> result<<<self graphicscontext>::resources resources>::shader,                   shader::createerror>;      /// creates shader program object     fn create_shader_program<'a>(&'a self, shaders: vec<&shader::shader>)         -> result<box<shader::shaderprogram + 'a>, shader::programcreateerror>;      // more come  } 

this create_shader_program method (and other methods) know concrete type of shader can call implementation specific methods on shader object.

i don't want put these methods (such setcurrent or attach) trait implementations must use. not graphics apis use same system: opengl bind/unbind, vulkan structs/set fields, directx else etc.

firstly, ask right way structure engine. believe in framework/application level code require these shader objects, can specify concrete type based upon current type of context.

// doesn't compile, should possible in theory // i'm trying say: // type of `shader` argument must match associated type // contained within `context` fn do_something_with_shader(context: &graphicscontext,                             shader: ??**graphicscontext::resources::shader**??)                            -> result<foo, bar>; 

or perhaps:

fn do_something_with_shader<t>(context: &graphicscontext<t>,                                shader: ??**t::shader**??)     t: graphicscontext::resources -> result<foo, bar>; 

is possible? can see sort of understand basic generics (i come java), driving me insane (it feel very hackish).

if right way go, there comes problem implementation. rustc wants associated type have lifetime specified.

wrong number of lifetime parameters: expected 1, found 0 [e0107] opal_driver_gl/src/context.rs:23     type shader = shader; 

my openglshader struct of type openglshader<'a>, error makes sense. question lifetime in bunch of code:

struct openglresources;  impl resources openglresources {     type shader = openglshader; }  impl graphicscontext openglgraphicscontext {      type resources = resources;      /// creates shader object     fn create_shader<'a>(&'a self, stage: core_shader::stage, source: &str)         -> result<<<self graphicscontext>::resources resources>::shader,                   core_shader::createerror> {        // impl goes here     }  } 

i tried attaching lifetime openglresources , openglgraphicscontext, solved error said error: parameter 'a never used.

so secondly, ask how can include lifetime inside associated type.

thanks if can take @ this. feel must possible checked @ compile time i'm pretty new rust can't quite grok how implement it.

i ended switching generics, provided successful implementation.

given resources<'a>, can define graphicscontext so:

trait graphicscontext<'a, r: resources<'a>> 

the 2 'a lifetimes above needed shader<'a> , shaderprogram<'a> structs inside resources

then opengl implementation can provided. note resources has been changed openglresources.

                             // replaced here impl<'a> graphicscontext<'a, openglresources<'a>> openglgraphicscontext<'a> {      fn create_shader(&'a self, ty: type, source: &str) -> result<shader<'a>, shadercreationerror> {         //     }  } 

Comments

Popular posts from this blog

How to connect android app to App engine -

gcc - MinGW's ld cannot perform PE operations on non PE output file -

php - display validation error message next to the textbox in codeigniter -