Tuesday, 15 March 2011

rust - Trait for enums convertible to isize -


I have several enums, each of them numeric tags and can be converted into integer ( isize < / Code>). I have a special quality that defines everyone. But the examples of those attributes can not be convertible to any more integer, because the compiler can not prove all the methods, in fact the parasailable nanams are.

Is there a feature which means that there is an enum with a parameter or it is variable for the integer (in any way) which I can use as an obstacle in generic, which receives numerical value need to?


To be more specific, here is a code sample ():

  enum Foo {a = 1, b = 2, c = 3, } Enum bar {d = 4, e = 5, f = 6} attribute process: std :: marker:: markerat {Fn process (V: icize) -> String; } Input process for Foo {FN process (v: isize) - & gt; String {format! ("Foo {}", v)}} times {FN process (v: isize) -> input process string {format! ("Bar {}", v)}} / just for logic; In reality, something more complex is, of course, fn extern_call (v: isize) - & gt; Eyesz {2 * v + 211} FN process & lt; T: Process & gt; (V: T) - & gt; String {T: Process (Execution of Viscus)} FN Main () {printen! ("A: {}", Process (Foo :: A)); }  

Obviously the problem is in the process ; failed to compile with v asize

Error: non-scalar cast: T as isize

So I would like to do something like this

 < Code> Process & lt; T: Process + Scalar & gt;  

or

  process & lt; T: process + tofrimitive & gt;  

Or something that will tell the compiler that the only type of permissible allowable, but I do not know how it might be.

Add a method for your property which combines it with a iisize . After this, the compiler can verify that the object meets the necessary criteria:

enum Foo {A = 1, B = 2, C = 3,} enum bar {D = 4, E = 5, F = 6,} Properties Process: {Fn as_isize (self) - & gt; Isize; FN Process (V: Isis) - & gt; String; } Foo {Fn as_isize (self) - & gt; Isaacs (F Isaac's) FN Process (V: Isaacs) - & gt; String {format! ("Foo {}", v)}} Process for the bar {fn as_isize (self) - & gt; Isaacs (F Isaac's) FN Process (V: Isaacs) - & gt; String {format! ("Bar {}", v)}} / just for logic; In reality, something more complex is, of course, fn extern_call (v: isize) - & gt; Eyesz {2 * v + 211} FN process & lt; T: Process & gt; (V: T) - & gt; String {T :: Process (extern_call (v.as_isize ()}} fn Main () {println! ("A: {}", Process (Foo :: A));}

FYI, MarkerTradit is not required here (and it is non-idiomatic):

The purpose of marker-practice is

is used as a supertrate for those symptoms, in which there is no way

No comments:

Post a Comment