[ 
https://issues.apache.org/jira/browse/AVRO-3759?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17724321#comment-17724321
 ] 

Fedor Telnov commented on AVRO-3759:
------------------------------------

I don't think any serious papers exist about any community-built Rust pattern :)

I'll try to show the benefit better myself:

Let's say you have a big compound enum just like we have Schema currently:

```

enum AnimalsEnum {

Dog \{name: String, surname: String},

Cat \{eyes_color: String},

....

}

```

Now you want your function to accept only Cats as an argument. How would you 
write it using current implementation? No way.

If we can't modify Cat variant, we could do something like that:

```

// Duplicate fields of enum variant

struct Cat {

 eyes_color: String,...

}

impl TryFrom<AnimalsEnum> for Cat {

 fn try_from(big_enum: AnimalsEnum) -> Cat {

    match big_enum {

Cat \{..} => Self \{..},

_ => panic!("NOT CAT");

}

}

}

fn function_receiving_cats(cat: Cat) \{...}

 

function_receiving_cats(AnimalsEnum::Cat \{...}.try_into())

```

And finally we use Cat struct as an argument to functions by transforming our 
enum to the Cat instance every time. That's what your users do ATM. We write 
replacements for, for instance, Record variant and use that replacement as an 
arguments to functions and write such conversion implementation.

So, here is what proposed - wrap your struct-based variants into NewType. That 
would allow users of your library to use Schema more conveniently.

> [Rust] Schema types inconsistency
> ---------------------------------
>
>                 Key: AVRO-3759
>                 URL: https://issues.apache.org/jira/browse/AVRO-3759
>             Project: Apache Avro
>          Issue Type: Improvement
>          Components: rust
>            Reporter: Fedor Telnov
>            Priority: Major
>
> That is how Unions and Records are defined in apache-avro Rust crate:
>  
> ```
>     /// A `union` Avro schema.
>     Union(UnionSchema),
>     /// A `record` Avro schema.
>     ///
>     /// The `lookup` table maps field names to their position in the `Vec`
>     /// of `fields`.
>     Record {
>         name: Name,
>         aliases: Aliases,
>         doc: Documentation,
>         fields: Vec<RecordField>,
>         lookup: BTreeMap<String, usize>,
>     },
> ```
>  
> That is inconsistent - one variant is defined with pattern Type(Type) which 
> is VERY convenient(for instance, it allows one to use UnionSchema as type), 
> and the other one(Record) is simply a struct variant, which is not very 
> convenient. My proposition is to hide Record's body in RecordSchema type and 
> use it here - just as you do with union. That would significantly help users, 
> as we can't use enum variants as types in Rust. It can also be done with 
> other schema types as well.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to