Giter Site home page Giter Site logo

Document Enumerations about prost HOT 5 CLOSED

tokio-rs avatar tokio-rs commented on May 21, 2024 23
Document Enumerations

from prost.

Comments (5)

danburkert avatar danburkert commented on May 21, 2024 1

Forgot to mention - there are other helper methods as well. Wherever an Enumeration is used as a field in a Message, the message will have an 'accessor' methods to get/set the value as the proper type. For instance, the Field struct contains a Cardinality field and the following methods:

impl Field {
  pub fn cardinality(&self) -> Cardinality;
  pub fn set_cardinality(&mut self, value: Cardinality);
}

Note that the first of these will return the default value (the first variant if otherwise unspecified) if the i32 value is out of range.

from prost.

danburkert avatar danburkert commented on May 21, 2024

Hi @dvtomas, thanks for opening this issue, I agree the docs should be improved in this area. The ergonomic helpers for enums were added after I wrote that bit in the readme, and I never went back and filled it in.

Toward your immediate issue, the best way to see what's available is to run rustdoc on the types and see the methods which they expose. In the case of a PhoneType enum, it will expose a pair of methods which help convert from i32:

impl PhoneType {
  pub fn is_valid(value: i32) -> bool;
  pub fn from_i32(value: i32) -> Option<PhoneType>;
}

You can see a concrete example here.

As for the Enumeration trait, there is actually no such thing. derive(Enumeration) is a trick which allows prost to generate these methods for enum types, but there's no corresponding trait.

I'm going to leave this issue open to track improving the enum docs.

from prost.

dvtomas avatar dvtomas commented on May 21, 2024

Thank you for the info! It's a neat trick to use rustdoc, I didn't know it also documents derived and macro-generated code.

from prost.

sdbondi avatar sdbondi commented on May 21, 2024

How about From<i32> be implemented for enums?

So that this would work:

match phone_type.into() {
   PhoneType::Mobile => {},
  // etc.    
  _ => {}
}

Methods like from_xxx are often implemented as From<Xxx> in rust.

EDIT: Just realised that from_i32 returns an Option. TryFrom<i32> could work but isn't as ergonomic

match phone_type.try_into().ok() {
   Some(PhoneType::Mobile) => {},
  // etc.    
  _ => {}
}

I see why this wasn't implemented this way - nevermind :)

from prost.

markmandel avatar markmandel commented on May 21, 2024

Can someone please provide an example on how to do do the rustdoc trick? I'm scouring documentation, and I can't seem to find the right combination of arguments 😞

I was hoping that cargo doc --open --document-private-items would do the job, but seems like it's not the case.

(Although the comments above definitely helped!, so thank you for that!)

from prost.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.