-
Notifications
You must be signed in to change notification settings - Fork 227
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
Use core
and alloc
crates for no_std
compatibility
#980
Changes from all commits
aa6f742
c87eb40
179daa2
0057f39
68dc199
93aed55
105e676
08c2429
fa47ec0
1637a23
cb86797
2310580
e6e51c4
1f95db5
42c6022
cced562
21303b2
701f028
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,18 @@ | ||
//! tendermint-proto library gives the developer access to the Tendermint proto-defined structs. | ||
|
||
#![no_std] | ||
#![deny(warnings, trivial_casts, trivial_numeric_casts, unused_import_braces)] | ||
#![allow(clippy::large_enum_variant)] | ||
#![forbid(unsafe_code)] | ||
#![doc(html_root_url = "https://docs.rs/tendermint-proto/0.22.0")] | ||
|
||
extern crate alloc; | ||
|
||
#[cfg(feature = "std")] | ||
extern crate std; | ||
|
||
mod prelude; | ||
|
||
/// Built-in prost_types with slight customization to enable JSON-encoding | ||
#[allow(warnings)] | ||
pub mod google { | ||
|
@@ -23,13 +31,15 @@ pub use error::Error; | |
pub use tendermint::*; | ||
|
||
use bytes::{Buf, BufMut}; | ||
use core::convert::{TryFrom, TryInto}; | ||
use core::fmt::Display; | ||
use prost::encoding::encoded_len_varint; | ||
use prost::Message; | ||
use std::convert::{TryFrom, TryInto}; | ||
use std::fmt::Display; | ||
|
||
pub mod serializers; | ||
|
||
use prelude::*; | ||
|
||
/// Allows for easy Google Protocol Buffers encoding and decoding of domain | ||
/// types with validation. | ||
/// | ||
|
@@ -38,7 +48,7 @@ pub mod serializers; | |
/// ```rust | ||
/// use bytes::BufMut; | ||
/// use prost::Message; | ||
/// use std::convert::TryFrom; | ||
/// use core::convert::TryFrom; | ||
/// use tendermint_proto::Protobuf; | ||
/// | ||
/// // This struct would ordinarily be automatically generated by prost. | ||
|
@@ -107,9 +117,15 @@ pub mod serializers; | |
/// // We expect a validation error here | ||
/// assert!(MyDomainType::decode(invalid_raw_bytes.as_ref()).is_err()); | ||
/// ``` | ||
pub trait Protobuf<T: Message + From<Self> + Default> | ||
pub trait Protobuf<T> | ||
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. I have modified the 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. Originally the idea here was that we wanted conversions from Changing this would imply that we could possibly have So then the practical consequences of this architectural change would be:
This seems like a lot of work/code just to be able to cater for the |
||
where | ||
Self: Sized + Clone + TryFrom<T>, | ||
T: Message, | ||
T: Default, | ||
Self: Sized, | ||
Self: Clone, | ||
T: TryFrom<Self>, | ||
Self: TryFrom<T>, | ||
<T as TryFrom<Self>>::Error: Display, | ||
<Self as TryFrom<T>>::Error: Display, | ||
{ | ||
/// Encode into a buffer in Protobuf format. | ||
|
@@ -119,7 +135,8 @@ where | |
/// | ||
/// [`prost::Message::encode`]: https://docs.rs/prost/*/prost/trait.Message.html#method.encode | ||
fn encode<B: BufMut>(&self, buf: &mut B) -> Result<(), Error> { | ||
T::from(self.clone()) | ||
T::try_from(self.clone()) | ||
.map_err(Error::try_from::<Self, T, _>)? | ||
.encode(buf) | ||
.map_err(Error::encode_message) | ||
} | ||
|
@@ -133,7 +150,8 @@ where | |
/// | ||
/// [`prost::Message::encode_length_delimited`]: https://docs.rs/prost/*/prost/trait.Message.html#method.encode_length_delimited | ||
fn encode_length_delimited<B: BufMut>(&self, buf: &mut B) -> Result<(), Error> { | ||
T::from(self.clone()) | ||
T::try_from(self.clone()) | ||
.map_err(Error::try_from::<Self, T, _>)? | ||
.encode_length_delimited(buf) | ||
.map_err(Error::encode_message) | ||
} | ||
|
@@ -173,13 +191,15 @@ where | |
/// counterpart Protobuf data structure. | ||
/// | ||
/// [`prost::Message::encoded_len`]: https://docs.rs/prost/*/prost/trait.Message.html#method.encoded_len | ||
fn encoded_len(&self) -> usize { | ||
T::from(self.clone()).encoded_len() | ||
fn encoded_len(&self) -> Result<usize, Error> { | ||
Ok(T::try_from(self.clone()) | ||
.map_err(Error::try_from::<Self, T, _>)? | ||
.encoded_len()) | ||
} | ||
|
||
/// Encodes into a Protobuf-encoded `Vec<u8>`. | ||
fn encode_vec(&self) -> Result<Vec<u8>, Error> { | ||
let mut wire = Vec::with_capacity(self.encoded_len()); | ||
let mut wire = Vec::with_capacity(self.encoded_len()?); | ||
self.encode(&mut wire).map(|_| wire) | ||
} | ||
|
||
|
@@ -191,7 +211,7 @@ where | |
|
||
/// Encode with a length-delimiter to a `Vec<u8>` Protobuf-encoded message. | ||
fn encode_length_delimited_vec(&self) -> Result<Vec<u8>, Error> { | ||
let len = self.encoded_len(); | ||
let len = self.encoded_len()?; | ||
let lenu64 = len.try_into().map_err(Error::parse_length)?; | ||
let mut wire = Vec::with_capacity(len + encoded_len_varint(lenu64)); | ||
self.encode_length_delimited(&mut wire).map(|_| wire) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
pub use core::prelude::v1::*; | ||
|
||
// Re-export according to alloc::prelude::v1 because it is not yet stabilized | ||
// https://doc.rust-lang.org/src/alloc/prelude/v1.rs.html | ||
pub use alloc::borrow::ToOwned; | ||
pub use alloc::boxed::Box; | ||
pub use alloc::string::{String, ToString}; | ||
pub use alloc::vec::Vec; | ||
|
||
pub use alloc::format; | ||
pub use alloc::vec; |
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.
It seems like there are some changes the Rust nightly that cause comments to be formatted slightly differently by
cargo fmt
.