Is there any way to put complex conditions and constraints on generic type in Rust -


let me explain in form of pseudo example:

trait {} trait b {} trait c {} struct d<t, x> if t: x: not b else if x: b t: c {} 

i've found way pass this, want way language features.

as @chris emerson requested:

more explanation:

let me explain it, in context of project (it small ray tracer renderer).

for example have vertex type has position , normal. have vertex type position , normal , texture coordinate.

and in other hand have material type work specific vertex type.

i want make constraint check on of functions use vertex , materials example:

fn do_something<v, m>(/* arguments */)      -> /* returned value */               if v: vertexwithtexturecoordinate m: materialwithtexture          else if v: simplevertex m: simplematerial { } 

this simplest condition facing.

i'm new in rust, maybe approach not good.

it's not possible. first of all: negative trait bounds (saying not trait foo) not exist in rust yet. there few rfcs, afaik there no specific plans on implementing in near future. however, specialization enable simulate negative trait bounds.

rust's type system turing complete, far can tell, want not possible without using specialization , negative trait bounds, both of not implemented/not stable.

but: ask pretty general case. there cases it's possible. if have struct d , want add methods it, need trait bounds, can write 2 impl blocks:

impl<t, x> d<t, x> t: a, x: notb {  // assume `notb` trait     // ... }  impl<t, x> d<t, x> t: c, x: b {     // ... } 

if think it, have write 2 implementations anyway: when t a, can use methods of a on objects of t; when t c, can use methods of c on objects of t. can't tell compiler "either a or c".

edit: solution specific problem

in case create trait denotes combination of vertex , material works together. like:

trait vertexmaterialpair {     // ... }  impl<v, m> vertexmaterialpair (v, m)      v: vertexwithtexturecoordinate,            m: materialwithtexture, { /* ... */ }  impl<v, m> vertexmaterialpair (v, m)      v: simplevertex,            m: simplematerial,  { /* ... */ } 

as can see implemented trait pair (tuple) of vertex , material. function like:

fn do_something<v, m>(/* arguments */)  -> /* ... */      (v, m): vertexmaterialpair  { /* ... */ } 

this should work well; however, might not best solution ray tracer...


Comments

Popular posts from this blog

PySide and Qt Properties: Connecting signals from Python to QML -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

scala - 'wrong top statement declaration' when using slick in IntelliJ -