Comments (5)
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.
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.
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.
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.
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)
- Consistent JSON serialisation HOT 1
- include_file does not work if any of the modules does not declare a package HOT 1
- Module generated for 'oneof' field conflicts with module generated for package
- cargo bench --workspace fails
- Update heck to v0.5?
- Sundry refactoring in code generator (ground work for builders) HOT 1
- Consider placing `Config::format` behind a feature flag.
- `prost-types` crate missing Well Known Types from google.protobuf.wrapper HOT 1
- feature: Deterministic Serialization HOT 2
- How to customize grpc method uri HOT 1
- `proto3` doesn't pack repeated fields by default HOT 2
- Generate `From<>` cast automatically for `protobuf::one`
- Unable to import proto from another library HOT 1
- #[deprecated] decorator is missing on enum access functions
- required field being deserialized as empty string
- prost-build: Allow path globbing for type and field attributes HOT 1
- .
- Rename prost-derive feature to derive
- Implement Ord & PartialOrd for prost_types::Timestamp
- Use of "Self" in stripped enum variants HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from prost.