-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixed Getting Started #1
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,11 +39,11 @@ The `micropb` project consists of two crates: | |
Add `micropb` crates to your `Cargo.toml`: | ||
```toml | ||
[dependencies] | ||
micropb = "0.1" | ||
# Allow types from `heapless` to be used for container fields | ||
micropb = { version = "0.1.0", features = ["container-heapless"] } | ||
|
||
[build-dependencies] | ||
# Allow types from `heapless` to be used for container fields | ||
micropb-gen = { version = "0.1", features = ["container-heapless"] } | ||
micropb-gen = "0.1.0" | ||
``` | ||
|
||
`micropb-gen` requires `protoc` to build `.proto` files, so [install `protoc`](https://grpc.io/docs/protoc-installation) and add it to your PATH, then invoke the code generator in `build.rs`: | ||
|
@@ -55,11 +55,34 @@ fn main() { | |
} | ||
``` | ||
|
||
Add `example.proto` to the project root directory: | ||
```proto | ||
syntax = "proto3"; | ||
|
||
package example; | ||
|
||
message Example { | ||
int32 f_int32 = 1; | ||
int64 f_int64 = 2; | ||
uint32 f_uint32 = 3; | ||
uint64 f_uint64 = 4; | ||
sint32 f_sint32 = 5; | ||
sint64 f_sint64 = 6; | ||
bool f_bool = 7; | ||
fixed32 f_fixed32 = 8; | ||
fixed64 f_fixed64 = 9; | ||
sfixed32 f_sfixed32 = 10; | ||
sfixed64 f_sfixed64 = 11; | ||
float f_float = 12; | ||
double f_double = 13; | ||
Comment on lines
+65
to
+77
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't need all these fields. Just put a comment saying something like "fields here" |
||
} | ||
``` | ||
|
||
Finally, include the generated file in your code: | ||
```rust,ignore | ||
// main.rs | ||
|
||
use micropb::{PbRead, PbDecoder, MessageDecode, MessageEncode}; | ||
use micropb::{MessageDecode, MessageEncode, PbDecoder, PbEncoder}; | ||
|
||
mod example { | ||
#![allow(clippy::all)] | ||
|
@@ -70,22 +93,30 @@ mod example { | |
} | ||
|
||
fn main() { | ||
let mut example = example::Example::default(); | ||
// Initialize an example struct | ||
let mut example0 = example::example_::Example::default(); | ||
example0.f_bool = true; | ||
example0.f_double = 17.4; | ||
|
||
// Use heapless::Vec as the output stream and build an encoder around it | ||
let mut encoder = PbEncoder::new(micropb::heapless::Vec::<u8, 16>::new()); | ||
|
||
// Compute the size of the `Example` on the wire | ||
let size = example0.compute_size(); | ||
println!("size: {}", size); | ||
// Encode the `Example` to the data stream | ||
example0.encode(&mut encoder).expect("Vec over capacity"); | ||
let data: &[u8] = encoder.as_writer().as_slice(); | ||
|
||
let data: &[u8] = &[ /* Protobuf data bytes */ ]; | ||
// Construct new decoder from byte slice | ||
let mut decoder = PbDecoder::new(data); | ||
|
||
let mut example = example::example_::Example::default(); | ||
|
||
// Decode a new instance of `Example` into an existing struct | ||
example.decode(&mut decoder, data.len()).expect("decoding failed"); | ||
|
||
// Use heapless::Vec as the output stream and build an encoder around it | ||
let mut encoder = PbEncoder::new(micropb::heapless::Vec::<u8, 10>::new()); | ||
|
||
// Compute the size of the `Example` on the wire | ||
let size = example.compute_size(); | ||
// Encode the `Example` to the data stream | ||
example.encode(&mut encoder).expect("Vec over capacity"); | ||
println!("decoded: {:?}", example); | ||
} | ||
``` | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't need those 2 lines