Skip to content

Instantly share code, notes, and snippets.

@cholcombe973
Last active January 29, 2016 19:39
Show Gist options
  • Save cholcombe973/e200c877dbf450e4df7d to your computer and use it in GitHub Desktop.
Save cholcombe973/e200c877dbf450e4df7d to your computer and use it in GitHub Desktop.
Versioned structs
macro_rules! cond_with_error(
($i:expr, $cond:expr, $submac:ident!( $($args:tt)* )) => (
{
if $cond {
match $submac!($i, $($args)*) {
nom::IResult::Done(i,o) => nom::IResult::Done(i, ::std::option::Option::Some(o)),
nom::IResult::Error(e) => nom::IResult::Error(e),
nom::IResult::Incomplete(i) => nom::IResult::Incomplete(i)
}
} else {
nom::IResult::Done($i, ::std::option::Option::None)
}
}
);
($i:expr, $cond:expr, $f:expr) => (
cond!($i, $cond, call!($f));
);
);
#[derive(RustcEncodable,Debug,Eq,PartialEq)]
pub struct Mosdpgcreate {
pub epoch: u64,
pub mkpg: Option<Vec<(pg_t, PgCreateT)>>,
pub ctimes: Vec<(pg_t, Utime)>,
}
impl<'a> CephPrimitiveVersioned<'a> for Mosdpgcreate {
fn read_from_wire(input: &'a [u8], head_version: u16, compat_version: u16) -> nom::IResult<&[u8], Self> {
let head_version = 2;
let compat_version = 1;
chain!(input,
epoch: le_u64 ~
count: le_u32 ~
mkpg: cond_with_error!(head_version >=2,
count!(
pair!(
call!(pg_t::read_from_wire, head_version, compat_version),
call!(PgCreateT::read_from_wire, head_version, compat_version)), count as usize)) ~
count: le_u32 ~
ctimes: count!(
pair!(
call!(pg_t::read_from_wire, head_version, compat_version),
call!(Utime::read_from_wire, head_version, compat_version)), count as usize) ,
||{
Mosdpgcreate{
epoch: epoch,
mkpg: mkpg,
ctimes: ctimes,
}
})
}
fn write_to_wire(&self) -> Result<Vec<u8>, SerialError> {
let buffer: Vec<u8> = Vec::new();
return Ok(buffer);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment