2014-07-18 13:56:17 +00:00
|
|
|
// Rust Bitcoin Library
|
|
|
|
// Written in 2014 by
|
2015-04-07 01:51:11 +00:00
|
|
|
// Andrew Poelstra <apoelstra@wpsoftware.net>
|
2014-07-18 13:56:17 +00:00
|
|
|
//
|
|
|
|
// To the extent possible under law, the author(s) have dedicated all
|
|
|
|
// copyright and related and neighboring rights to this software to
|
|
|
|
// the public domain worldwide. This software is distributed without
|
|
|
|
// any warranty.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the CC0 Public Domain Dedication
|
|
|
|
// along with this software.
|
|
|
|
// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
|
|
|
|
//
|
|
|
|
|
2015-01-18 18:16:01 +00:00
|
|
|
macro_rules! impl_consensus_encoding {
|
2015-04-07 01:51:11 +00:00
|
|
|
($thing:ident, $($field:ident),+) => (
|
Move relevant names into consensus::encode
- Move network::encodable::* to consensus::encode::*
- Rename Consensus{En,De}codable to {En,De}codable (now under
consensus::encode)
- Move network::serialize::Error to consensus::encode::Error
- Remove Raw{En,De}coder, implement {En,De}coder for T: {Write,Read}
instead
- Move network::serialize::Simple{En,De}coder to
consensus::encode::{En,De}coder
- Rename util::Error::Serialize to util::Error::Encode
- Modify comments to refer to new names
- Modify files to refer to new names
- Expose {En,De}cod{able,er}, {de,}serialize, Params
- Do not return Result for serialize{,_hex} as serializing to a Vec
should never fail
2018-09-20 10:15:45 +00:00
|
|
|
impl<S: ::consensus::encode::Encoder> ::consensus::encode::Encodable<S> for $thing {
|
2015-04-07 01:51:11 +00:00
|
|
|
#[inline]
|
Move relevant names into consensus::encode
- Move network::encodable::* to consensus::encode::*
- Rename Consensus{En,De}codable to {En,De}codable (now under
consensus::encode)
- Move network::serialize::Error to consensus::encode::Error
- Remove Raw{En,De}coder, implement {En,De}coder for T: {Write,Read}
instead
- Move network::serialize::Simple{En,De}coder to
consensus::encode::{En,De}coder
- Rename util::Error::Serialize to util::Error::Encode
- Modify comments to refer to new names
- Modify files to refer to new names
- Expose {En,De}cod{able,er}, {de,}serialize, Params
- Do not return Result for serialize{,_hex} as serializing to a Vec
should never fail
2018-09-20 10:15:45 +00:00
|
|
|
fn consensus_encode(&self, s: &mut S) -> Result<(), ::consensus::encode::Error> {
|
2018-08-12 16:47:31 +00:00
|
|
|
$( self.$field.consensus_encode(s)?; )+
|
2015-04-07 01:51:11 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2014-07-18 13:56:17 +00:00
|
|
|
|
Move relevant names into consensus::encode
- Move network::encodable::* to consensus::encode::*
- Rename Consensus{En,De}codable to {En,De}codable (now under
consensus::encode)
- Move network::serialize::Error to consensus::encode::Error
- Remove Raw{En,De}coder, implement {En,De}coder for T: {Write,Read}
instead
- Move network::serialize::Simple{En,De}coder to
consensus::encode::{En,De}coder
- Rename util::Error::Serialize to util::Error::Encode
- Modify comments to refer to new names
- Modify files to refer to new names
- Expose {En,De}cod{able,er}, {de,}serialize, Params
- Do not return Result for serialize{,_hex} as serializing to a Vec
should never fail
2018-09-20 10:15:45 +00:00
|
|
|
impl<D: ::consensus::encode::Decoder> ::consensus::encode::Decodable<D> for $thing {
|
2015-04-07 01:51:11 +00:00
|
|
|
#[inline]
|
Move relevant names into consensus::encode
- Move network::encodable::* to consensus::encode::*
- Rename Consensus{En,De}codable to {En,De}codable (now under
consensus::encode)
- Move network::serialize::Error to consensus::encode::Error
- Remove Raw{En,De}coder, implement {En,De}coder for T: {Write,Read}
instead
- Move network::serialize::Simple{En,De}coder to
consensus::encode::{En,De}coder
- Rename util::Error::Serialize to util::Error::Encode
- Modify comments to refer to new names
- Modify files to refer to new names
- Expose {En,De}cod{able,er}, {de,}serialize, Params
- Do not return Result for serialize{,_hex} as serializing to a Vec
should never fail
2018-09-20 10:15:45 +00:00
|
|
|
fn consensus_decode(d: &mut D) -> Result<$thing, ::consensus::encode::Error> {
|
|
|
|
use consensus::encode::Decodable;
|
2015-04-07 01:51:11 +00:00
|
|
|
Ok($thing {
|
Move relevant names into consensus::encode
- Move network::encodable::* to consensus::encode::*
- Rename Consensus{En,De}codable to {En,De}codable (now under
consensus::encode)
- Move network::serialize::Error to consensus::encode::Error
- Remove Raw{En,De}coder, implement {En,De}coder for T: {Write,Read}
instead
- Move network::serialize::Simple{En,De}coder to
consensus::encode::{En,De}coder
- Rename util::Error::Serialize to util::Error::Encode
- Modify comments to refer to new names
- Modify files to refer to new names
- Expose {En,De}cod{able,er}, {de,}serialize, Params
- Do not return Result for serialize{,_hex} as serializing to a Vec
should never fail
2018-09-20 10:15:45 +00:00
|
|
|
$( $field: Decodable::consensus_decode(d)?, )+
|
2015-04-07 01:51:11 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2015-01-18 18:16:01 +00:00
|
|
|
}
|
2014-07-18 13:56:17 +00:00
|
|
|
|
2015-01-18 18:16:01 +00:00
|
|
|
macro_rules! impl_newtype_consensus_encoding {
|
2015-04-07 01:51:11 +00:00
|
|
|
($thing:ident) => (
|
Move relevant names into consensus::encode
- Move network::encodable::* to consensus::encode::*
- Rename Consensus{En,De}codable to {En,De}codable (now under
consensus::encode)
- Move network::serialize::Error to consensus::encode::Error
- Remove Raw{En,De}coder, implement {En,De}coder for T: {Write,Read}
instead
- Move network::serialize::Simple{En,De}coder to
consensus::encode::{En,De}coder
- Rename util::Error::Serialize to util::Error::Encode
- Modify comments to refer to new names
- Modify files to refer to new names
- Expose {En,De}cod{able,er}, {de,}serialize, Params
- Do not return Result for serialize{,_hex} as serializing to a Vec
should never fail
2018-09-20 10:15:45 +00:00
|
|
|
impl<S: ::consensus::encode::Encoder> ::consensus::encode::Encodable<S> for $thing {
|
2015-04-07 01:51:11 +00:00
|
|
|
#[inline]
|
Move relevant names into consensus::encode
- Move network::encodable::* to consensus::encode::*
- Rename Consensus{En,De}codable to {En,De}codable (now under
consensus::encode)
- Move network::serialize::Error to consensus::encode::Error
- Remove Raw{En,De}coder, implement {En,De}coder for T: {Write,Read}
instead
- Move network::serialize::Simple{En,De}coder to
consensus::encode::{En,De}coder
- Rename util::Error::Serialize to util::Error::Encode
- Modify comments to refer to new names
- Modify files to refer to new names
- Expose {En,De}cod{able,er}, {de,}serialize, Params
- Do not return Result for serialize{,_hex} as serializing to a Vec
should never fail
2018-09-20 10:15:45 +00:00
|
|
|
fn consensus_encode(&self, s: &mut S) -> Result<(), ::consensus::encode::Error> {
|
2015-04-07 01:51:11 +00:00
|
|
|
let &$thing(ref data) = self;
|
|
|
|
data.consensus_encode(s)
|
|
|
|
}
|
|
|
|
}
|
2014-08-22 18:32:42 +00:00
|
|
|
|
Move relevant names into consensus::encode
- Move network::encodable::* to consensus::encode::*
- Rename Consensus{En,De}codable to {En,De}codable (now under
consensus::encode)
- Move network::serialize::Error to consensus::encode::Error
- Remove Raw{En,De}coder, implement {En,De}coder for T: {Write,Read}
instead
- Move network::serialize::Simple{En,De}coder to
consensus::encode::{En,De}coder
- Rename util::Error::Serialize to util::Error::Encode
- Modify comments to refer to new names
- Modify files to refer to new names
- Expose {En,De}cod{able,er}, {de,}serialize, Params
- Do not return Result for serialize{,_hex} as serializing to a Vec
should never fail
2018-09-20 10:15:45 +00:00
|
|
|
impl<D: ::consensus::encode::Decoder> ::consensus::encode::Decodable<D> for $thing {
|
2015-04-07 01:51:11 +00:00
|
|
|
#[inline]
|
Move relevant names into consensus::encode
- Move network::encodable::* to consensus::encode::*
- Rename Consensus{En,De}codable to {En,De}codable (now under
consensus::encode)
- Move network::serialize::Error to consensus::encode::Error
- Remove Raw{En,De}coder, implement {En,De}coder for T: {Write,Read}
instead
- Move network::serialize::Simple{En,De}coder to
consensus::encode::{En,De}coder
- Rename util::Error::Serialize to util::Error::Encode
- Modify comments to refer to new names
- Modify files to refer to new names
- Expose {En,De}cod{able,er}, {de,}serialize, Params
- Do not return Result for serialize{,_hex} as serializing to a Vec
should never fail
2018-09-20 10:15:45 +00:00
|
|
|
fn consensus_decode(d: &mut D) -> Result<$thing, ::consensus::encode::Error> {
|
|
|
|
Ok($thing(Decodable::consensus_decode(d)?))
|
2015-04-07 01:51:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2015-01-18 18:16:01 +00:00
|
|
|
}
|
2014-08-22 18:32:42 +00:00
|
|
|
|
2015-01-18 18:16:01 +00:00
|
|
|
macro_rules! impl_array_newtype {
|
2015-04-07 01:51:11 +00:00
|
|
|
($thing:ident, $ty:ty, $len:expr) => {
|
|
|
|
impl $thing {
|
|
|
|
#[inline]
|
|
|
|
/// Converts the object to a raw pointer
|
|
|
|
pub fn as_ptr(&self) -> *const $ty {
|
|
|
|
let &$thing(ref dat) = self;
|
|
|
|
dat.as_ptr()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
/// Converts the object to a mutable raw pointer
|
|
|
|
pub fn as_mut_ptr(&mut self) -> *mut $ty {
|
|
|
|
let &mut $thing(ref mut dat) = self;
|
|
|
|
dat.as_mut_ptr()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
/// Returns the length of the object as an array
|
|
|
|
pub fn len(&self) -> usize { $len }
|
2015-10-28 16:27:23 +00:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
/// Returns whether the object, as an array, is empty. Always false.
|
|
|
|
pub fn is_empty(&self) -> bool { false }
|
2018-05-16 09:31:59 +00:00
|
|
|
|
|
|
|
#[inline]
|
2018-08-20 21:20:43 +00:00
|
|
|
/// Returns the underlying bytes.
|
|
|
|
pub fn as_bytes(&self) -> &[$ty; $len] { &self.0 }
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
/// Returns the underlying bytes.
|
|
|
|
pub fn to_bytes(&self) -> [$ty; $len] { self.0.clone() }
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
/// Returns the underlying bytes.
|
|
|
|
pub fn into_bytes(self) -> [$ty; $len] { self.0 }
|
2015-10-14 13:56:48 +00:00
|
|
|
}
|
2015-04-07 01:51:11 +00:00
|
|
|
|
2015-10-14 13:56:48 +00:00
|
|
|
impl<'a> From<&'a [$ty]> for $thing {
|
|
|
|
fn from(data: &'a [$ty]) -> $thing {
|
2015-04-07 01:51:11 +00:00
|
|
|
assert_eq!(data.len(), $len);
|
2018-02-14 16:44:02 +00:00
|
|
|
let mut ret = [0; $len];
|
|
|
|
ret.copy_from_slice(&data[..]);
|
|
|
|
$thing(ret)
|
2015-04-07 01:51:11 +00:00
|
|
|
}
|
|
|
|
}
|
2014-08-28 16:49:03 +00:00
|
|
|
|
2015-04-05 03:13:19 +00:00
|
|
|
impl ::std::ops::Index<usize> for $thing {
|
|
|
|
type Output = $ty;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn index(&self, index: usize) -> &$ty {
|
|
|
|
let &$thing(ref dat) = self;
|
|
|
|
&dat[index]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-07 22:51:57 +00:00
|
|
|
impl_index_newtype!($thing, $ty);
|
2014-08-28 16:49:03 +00:00
|
|
|
|
2015-04-07 01:51:11 +00:00
|
|
|
impl PartialEq for $thing {
|
|
|
|
#[inline]
|
|
|
|
fn eq(&self, other: &$thing) -> bool {
|
|
|
|
&self[..] == &other[..]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Eq for $thing {}
|
2014-08-28 16:49:03 +00:00
|
|
|
|
2015-12-20 21:38:02 +00:00
|
|
|
impl PartialOrd for $thing {
|
|
|
|
#[inline]
|
|
|
|
fn partial_cmp(&self, other: &$thing) -> Option<::std::cmp::Ordering> {
|
|
|
|
Some(self.cmp(&other))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Ord for $thing {
|
|
|
|
#[inline]
|
|
|
|
fn cmp(&self, other: &$thing) -> ::std::cmp::Ordering {
|
|
|
|
// manually implement comparison to get little-endian ordering
|
|
|
|
// (we need this for our numeric types; non-numeric ones shouldn't
|
|
|
|
// be ordered anyway except to put them in BTrees or whatever, and
|
|
|
|
// they don't care how we order as long as we're consisistent).
|
|
|
|
for i in 0..$len {
|
|
|
|
if self[$len - 1 - i] < other[$len - 1 - i] { return ::std::cmp::Ordering::Less; }
|
|
|
|
if self[$len - 1 - i] > other[$len - 1 - i] { return ::std::cmp::Ordering::Greater; }
|
|
|
|
}
|
|
|
|
::std::cmp::Ordering::Equal
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-21 14:35:27 +00:00
|
|
|
#[cfg_attr(feature = "clippy", allow(expl_impl_clone_on_copy))] // we don't define the `struct`, we have to explicitly impl
|
2015-04-07 01:51:11 +00:00
|
|
|
impl Clone for $thing {
|
|
|
|
#[inline]
|
|
|
|
fn clone(&self) -> $thing {
|
2015-10-14 13:56:48 +00:00
|
|
|
$thing::from(&self[..])
|
2015-04-07 01:51:11 +00:00
|
|
|
}
|
|
|
|
}
|
2014-08-28 16:49:03 +00:00
|
|
|
|
2015-04-10 23:15:57 +00:00
|
|
|
impl Copy for $thing {}
|
|
|
|
|
2015-04-07 01:51:11 +00:00
|
|
|
impl ::std::hash::Hash for $thing {
|
|
|
|
#[inline]
|
|
|
|
fn hash<H>(&self, state: &mut H)
|
|
|
|
where H: ::std::hash::Hasher
|
|
|
|
{
|
|
|
|
(&self[..]).hash(state);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn hash_slice<H>(data: &[$thing], state: &mut H)
|
|
|
|
where H: ::std::hash::Hasher
|
|
|
|
{
|
|
|
|
for d in data.iter() {
|
|
|
|
(&d[..]).hash(state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-10-14 13:56:48 +00:00
|
|
|
|
|
|
|
impl ::rand::Rand for $thing {
|
|
|
|
#[inline]
|
|
|
|
fn rand<R: ::rand::Rng>(r: &mut R) -> $thing {
|
|
|
|
$thing(::rand::Rand::rand(r))
|
|
|
|
}
|
|
|
|
}
|
2014-08-28 16:49:03 +00:00
|
|
|
}
|
2015-01-18 18:16:01 +00:00
|
|
|
}
|
2014-08-28 16:49:03 +00:00
|
|
|
|
2015-01-18 18:16:01 +00:00
|
|
|
macro_rules! impl_array_newtype_encodable {
|
2015-04-07 01:51:11 +00:00
|
|
|
($thing:ident, $ty:ty, $len:expr) => {
|
2018-08-20 16:37:19 +00:00
|
|
|
#[cfg(feature = "serde")]
|
|
|
|
impl<'de> $crate::serde::Deserialize<'de> for $thing {
|
|
|
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
|
|
|
where
|
|
|
|
D: $crate::serde::Deserializer<'de>,
|
2015-04-07 01:51:11 +00:00
|
|
|
{
|
2018-08-20 16:37:19 +00:00
|
|
|
use $crate::std::fmt::{self, Formatter};
|
|
|
|
|
|
|
|
struct Visitor;
|
|
|
|
impl<'de> $crate::serde::de::Visitor<'de> for Visitor {
|
2015-04-07 01:51:11 +00:00
|
|
|
type Value = $thing;
|
|
|
|
|
2018-08-20 16:37:19 +00:00
|
|
|
fn expecting(&self, formatter: &mut Formatter) -> fmt::Result {
|
|
|
|
formatter.write_str("a fixed size array")
|
|
|
|
}
|
|
|
|
|
2015-04-07 01:51:11 +00:00
|
|
|
#[inline]
|
2018-08-20 16:37:19 +00:00
|
|
|
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
|
|
|
|
where
|
|
|
|
A: $crate::serde::de::SeqAccess<'de>,
|
2015-04-07 01:51:11 +00:00
|
|
|
{
|
2018-02-14 16:44:02 +00:00
|
|
|
let mut ret: [$ty; $len] = [0; $len];
|
|
|
|
for item in ret.iter_mut() {
|
2018-08-20 16:37:19 +00:00
|
|
|
*item = match seq.next_element()? {
|
2018-02-14 16:44:02 +00:00
|
|
|
Some(c) => c,
|
2018-08-20 16:37:19 +00:00
|
|
|
None => return Err($crate::serde::de::Error::custom("end of stream"))
|
2018-02-14 16:44:02 +00:00
|
|
|
};
|
2015-04-07 01:51:11 +00:00
|
|
|
}
|
2018-02-14 16:44:02 +00:00
|
|
|
Ok($thing(ret))
|
2015-04-07 01:51:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-20 16:37:19 +00:00
|
|
|
deserializer.deserialize_seq(Visitor)
|
2015-04-07 01:51:11 +00:00
|
|
|
}
|
|
|
|
}
|
2014-09-05 14:10:18 +00:00
|
|
|
|
2018-08-20 16:37:19 +00:00
|
|
|
#[cfg(feature = "serde")]
|
|
|
|
impl $crate::serde::Serialize for $thing {
|
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
|
where
|
|
|
|
S: $crate::serde::Serializer,
|
2015-04-07 01:51:11 +00:00
|
|
|
{
|
|
|
|
let &$thing(ref dat) = self;
|
2018-08-20 16:37:19 +00:00
|
|
|
(&dat[..]).serialize(serializer)
|
2015-04-07 01:51:11 +00:00
|
|
|
}
|
|
|
|
}
|
2014-09-05 14:10:18 +00:00
|
|
|
}
|
2015-01-18 18:16:01 +00:00
|
|
|
}
|
2014-09-05 14:10:18 +00:00
|
|
|
|
2015-01-18 18:16:01 +00:00
|
|
|
macro_rules! impl_array_newtype_show {
|
2015-04-07 01:51:11 +00:00
|
|
|
($thing:ident) => {
|
|
|
|
impl ::std::fmt::Debug for $thing {
|
|
|
|
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
|
|
|
|
write!(f, concat!(stringify!($thing), "({:?})"), &self[..])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-07 22:51:57 +00:00
|
|
|
macro_rules! impl_index_newtype {
|
|
|
|
($thing:ident, $ty:ty) => {
|
|
|
|
impl ::std::ops::Index<::std::ops::Range<usize>> for $thing {
|
|
|
|
type Output = [$ty];
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn index(&self, index: ::std::ops::Range<usize>) -> &[$ty] {
|
|
|
|
&self.0[index]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ::std::ops::Index<::std::ops::RangeTo<usize>> for $thing {
|
|
|
|
type Output = [$ty];
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn index(&self, index: ::std::ops::RangeTo<usize>) -> &[$ty] {
|
|
|
|
&self.0[index]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ::std::ops::Index<::std::ops::RangeFrom<usize>> for $thing {
|
|
|
|
type Output = [$ty];
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn index(&self, index: ::std::ops::RangeFrom<usize>) -> &[$ty] {
|
|
|
|
&self.0[index]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ::std::ops::Index<::std::ops::RangeFull> for $thing {
|
|
|
|
type Output = [$ty];
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn index(&self, _: ::std::ops::RangeFull) -> &[$ty] {
|
|
|
|
&self.0[..]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-07 01:51:11 +00:00
|
|
|
macro_rules! display_from_debug {
|
|
|
|
($thing:ident) => {
|
|
|
|
impl ::std::fmt::Display for $thing {
|
|
|
|
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> Result<(), ::std::fmt::Error> {
|
|
|
|
::std::fmt::Debug::fmt(self, f)
|
|
|
|
}
|
|
|
|
}
|
2014-08-28 16:49:03 +00:00
|
|
|
}
|
2015-01-18 18:16:01 +00:00
|
|
|
}
|
2014-08-28 16:49:03 +00:00
|
|
|
|
2018-01-15 17:54:32 +00:00
|
|
|
#[cfg(test)]
|
2018-07-24 18:43:03 +00:00
|
|
|
macro_rules! hex_script (($s:expr) => (::blockdata::script::Script::from(::hex::decode($s).unwrap())));
|
2018-01-15 17:54:32 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
2018-07-24 18:43:03 +00:00
|
|
|
macro_rules! hex_hash (($s:expr) => (::util::hash::Sha256dHash::from(&::hex::decode($s).unwrap()[..])));
|
2018-01-15 17:54:32 +00:00
|
|
|
|
2018-02-18 15:28:24 +00:00
|
|
|
macro_rules! serde_struct_impl {
|
2018-08-20 16:37:19 +00:00
|
|
|
($name:ident, $($fe:ident),*) => (
|
|
|
|
#[cfg(feature = "serde")]
|
|
|
|
impl<'de> $crate::serde::Deserialize<'de> for $name {
|
|
|
|
fn deserialize<D>(deserializer: D) -> Result<$name, D::Error>
|
|
|
|
where
|
|
|
|
D: $crate::serde::de::Deserializer<'de>,
|
2018-02-18 15:28:24 +00:00
|
|
|
{
|
2018-08-20 16:37:19 +00:00
|
|
|
use $crate::std::fmt::{self, Formatter};
|
|
|
|
use $crate::serde::de::IgnoredAny;
|
2018-02-18 15:28:24 +00:00
|
|
|
|
|
|
|
#[allow(non_camel_case_types)]
|
|
|
|
enum Enum { Unknown__Field, $($fe),* }
|
|
|
|
|
|
|
|
struct EnumVisitor;
|
2018-08-20 16:37:19 +00:00
|
|
|
impl<'de> $crate::serde::de::Visitor<'de> for EnumVisitor {
|
2018-02-18 15:28:24 +00:00
|
|
|
type Value = Enum;
|
|
|
|
|
2018-08-20 16:37:19 +00:00
|
|
|
fn expecting(&self, formatter: &mut Formatter) -> fmt::Result {
|
|
|
|
formatter.write_str("a field name")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
|
|
|
|
where
|
|
|
|
E: $crate::serde::de::Error,
|
2018-02-18 15:28:24 +00:00
|
|
|
{
|
2018-08-20 16:37:19 +00:00
|
|
|
match v {
|
2018-02-18 15:28:24 +00:00
|
|
|
$(
|
|
|
|
stringify!($fe) => Ok(Enum::$fe)
|
|
|
|
),*,
|
|
|
|
_ => Ok(Enum::Unknown__Field)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-20 16:37:19 +00:00
|
|
|
impl<'de> $crate::serde::Deserialize<'de> for Enum {
|
|
|
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
|
|
|
where
|
|
|
|
D: ::serde::de::Deserializer<'de>,
|
2018-02-18 15:28:24 +00:00
|
|
|
{
|
2018-08-20 16:37:19 +00:00
|
|
|
deserializer.deserialize_str(EnumVisitor)
|
2018-02-18 15:28:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Visitor;
|
|
|
|
|
2018-08-20 16:37:19 +00:00
|
|
|
impl<'de> $crate::serde::de::Visitor<'de> for Visitor {
|
2018-02-18 15:28:24 +00:00
|
|
|
type Value = $name;
|
|
|
|
|
2018-08-20 16:37:19 +00:00
|
|
|
fn expecting(&self, formatter: &mut Formatter) -> fmt::Result {
|
|
|
|
formatter.write_str("a struct")
|
2018-02-18 15:28:24 +00:00
|
|
|
}
|
|
|
|
|
2018-08-20 16:37:19 +00:00
|
|
|
fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
|
|
|
|
where
|
|
|
|
A: $crate::serde::de::MapAccess<'de>,
|
2018-02-18 15:28:24 +00:00
|
|
|
{
|
2018-08-20 16:37:19 +00:00
|
|
|
use $crate::serde::de::Error;
|
2018-02-18 15:28:24 +00:00
|
|
|
|
|
|
|
$(let mut $fe = None;)*
|
|
|
|
|
|
|
|
loop {
|
2018-08-20 16:37:19 +00:00
|
|
|
match map.next_key::<Enum>()? {
|
|
|
|
Some(Enum::Unknown__Field) => {
|
|
|
|
map.next_value::<IgnoredAny>()?;
|
|
|
|
}
|
|
|
|
$(
|
|
|
|
Some(Enum::$fe) => {
|
|
|
|
$fe = Some(map.next_value()?);
|
|
|
|
}
|
|
|
|
)*
|
2018-02-18 15:28:24 +00:00
|
|
|
None => { break; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$(
|
2018-08-20 16:37:19 +00:00
|
|
|
let $fe = match $fe {
|
|
|
|
Some(x) => x,
|
|
|
|
None => return Err(A::Error::missing_field(stringify!($fe))),
|
|
|
|
};
|
2018-02-18 15:28:24 +00:00
|
|
|
)*
|
2018-08-20 16:37:19 +00:00
|
|
|
|
|
|
|
let ret = $name {
|
|
|
|
$($fe: $fe),*
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(ret)
|
2018-02-18 15:28:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// end type defs
|
|
|
|
|
2018-08-20 16:37:19 +00:00
|
|
|
static FIELDS: &'static [&'static str] = &[$(stringify!($fe)),*];
|
2018-02-18 15:28:24 +00:00
|
|
|
|
2018-08-20 16:37:19 +00:00
|
|
|
deserializer.deserialize_struct(stringify!($name), FIELDS, Visitor)
|
2018-02-18 15:28:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-20 16:37:19 +00:00
|
|
|
#[cfg(feature = "serde")]
|
|
|
|
impl<'de> $crate::serde::Serialize for $name {
|
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
|
where
|
|
|
|
S: $crate::serde::Serializer,
|
2018-02-18 15:28:24 +00:00
|
|
|
{
|
2018-08-20 16:37:19 +00:00
|
|
|
use $crate::serde::ser::SerializeStruct;
|
2018-02-18 15:28:24 +00:00
|
|
|
|
2018-08-20 16:37:19 +00:00
|
|
|
// Only used to get the struct length.
|
|
|
|
static FIELDS: &'static [&'static str] = &[$(stringify!($fe)),*];
|
2018-02-18 15:28:24 +00:00
|
|
|
|
2018-08-20 16:37:19 +00:00
|
|
|
let mut st = serializer.serialize_struct(stringify!($name), FIELDS.len())?;
|
2018-02-18 15:28:24 +00:00
|
|
|
|
2018-08-20 16:37:19 +00:00
|
|
|
$(
|
|
|
|
st.serialize_field(stringify!($fe), &self.$fe)?;
|
|
|
|
)*
|
2018-02-18 15:28:24 +00:00
|
|
|
|
2018-08-20 16:37:19 +00:00
|
|
|
st.end()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
2018-02-18 15:28:24 +00:00
|
|
|
}
|