Skip to content

Commit

Permalink
implement (Un)marshall and Signature for f64
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxVerevkin committed Feb 18, 2024
1 parent 77414d7 commit c90aceb
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
35 changes: 35 additions & 0 deletions rustbus/src/params/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,13 @@ impl<'a, 'e> Param<'a, 'e> {
_ => Err(self),
}
}
pub fn into_f64(self) -> Result<f64, Param<'a, 'e>> {
match self {
Param::Base(Base::Double(s)) => Ok(f64::from_bits(s)),
Param::Base(Base::DoubleRef(s)) => Ok(f64::from_bits(*s)),
_ => Err(self),
}
}
}

//
Expand Down Expand Up @@ -355,6 +362,13 @@ impl<'a> Base<'a> {
_ => Err(self),
}
}
pub fn into_f64(self) -> Result<f64, Self> {
match self {
Base::Double(s) => Ok(f64::from_bits(s)),
Base::DoubleRef(s) => Ok(f64::from_bits(*s)),
_ => Err(self),
}
}
}

impl<'a> std::convert::From<&Base<'a>> for signature::Base {
Expand Down Expand Up @@ -499,6 +513,17 @@ impl<'a> std::convert::TryFrom<&Base<'a>> for i64 {
}
}

impl<'a> std::convert::TryFrom<&Base<'a>> for f64 {
type Error = ConversionError;
fn try_from(b: &Base) -> std::result::Result<f64, ConversionError> {
if let Base::Double(value) = b {
Ok(f64::from_bits(*value))
} else {
Err(ConversionError::InvalidType)
}
}
}

//
//
// Param TO
Expand Down Expand Up @@ -652,6 +677,11 @@ impl<'a> std::convert::From<i64> for Base<'a> {
Base::Int64(s)
}
}
impl<'a> std::convert::From<f64> for Base<'a> {
fn from(s: f64) -> Self {
Base::Double(s.to_bits())
}
}
impl<'a> std::convert::From<&'a bool> for Base<'a> {
fn from(s: &'a bool) -> Self {
Base::BooleanRef(s)
Expand All @@ -677,6 +707,11 @@ impl<'a> std::convert::From<&'a u64> for Base<'a> {
Base::Uint64Ref(s)
}
}
impl<'a> std::convert::From<&'a f64> for Base<'a> {
fn from(s: &'a f64) -> Self {
Base::Double(s.to_bits())
}
}
impl<'a> std::convert::From<&'a i16> for Base<'a> {
fn from(s: &'a i16) -> Self {
Base::Int16Ref(s)
Expand Down
28 changes: 28 additions & 0 deletions rustbus/src/wire/marshal/traits/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,34 @@ impl Marshal for bool {
}
}

impl Signature for f64 {
#[inline]
fn signature() -> crate::signature::Type {
crate::signature::Type::Base(crate::signature::Base::Double)
}
#[inline]
fn alignment() -> usize {
8
}
#[inline]
unsafe fn valid_slice(bo: crate::ByteOrder) -> bool {
bo == crate::ByteOrder::NATIVE
}
fn sig_str(sig: &mut SignatureBuffer) {
sig.push_static("d");
}
fn has_sig(sig: &str) -> bool {
sig.starts_with('d')
}
}
impl Marshal for f64 {
fn marshal(&self, ctx: &mut MarshalContext) -> Result<(), MarshalError> {
ctx.align_to(Self::alignment());
util::write_u64(self.to_bits(), ctx.byteorder, ctx.buf);
Ok(())
}
}

impl Signature for String {
#[inline]
fn signature() -> crate::signature::Type {
Expand Down
9 changes: 9 additions & 0 deletions rustbus/src/wire/unmarshal/traits/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,15 @@ impl<'buf, 'fds> Unmarshal<'buf, 'fds> for bool {
}
}

impl<'buf, 'fds> Unmarshal<'buf, 'fds> for f64 {
fn unmarshal(ctx: &mut UnmarshalContext<'fds, 'buf>) -> unmarshal::UnmarshalResult<Self> {
let padding = ctx.align_to(Self::alignment())?;
let (bytes, val) = util::parse_u64(&ctx.buf[ctx.offset..], ctx.byteorder)?;
ctx.offset += bytes;
Ok((bytes + padding, f64::from_bits(val)))
}
}

impl<'buf, 'fds> Unmarshal<'buf, 'fds> for &'buf str {
fn unmarshal(ctx: &mut UnmarshalContext<'fds, 'buf>) -> unmarshal::UnmarshalResult<Self> {
let padding = ctx.align_to(Self::alignment())?;
Expand Down

0 comments on commit c90aceb

Please sign in to comment.