use crate::{Error, Marker, WriteBuffer};

/// Encode a `f32` value.
pub fn write_f32(buf: &mut WriteBuffer, value: f32) -> Result<Marker, Error> {
    let marker = Marker::F32;
    buf.write(5, |buf| {
        // Safety: 5 bytes available in buf.
        unsafe {
            let buf = buf.as_mut_ptr();
            *buf = marker.encoding();

            let buf = buf.add(1);
            buf.copy_from_nonoverlapping(f32::to_be_bytes(value).as_ptr(), 4);
        }
    })?;

    Ok(marker)
}

/// Encode a `f64` value.
pub fn write_f64(buf: &mut WriteBuffer, value: f64) -> Result<Marker, Error> {
    let marker = Marker::F64;
    buf.write(9, |buf| {
        // Safety: 9 bytes available in buf.
        unsafe {
            let buf = buf.as_mut_ptr();
            *buf = marker.encoding();

            let buf = buf.add(1);
            buf.copy_from_nonoverlapping(f64::to_be_bytes(value).as_ptr(), 8);
        }
    })?;

    Ok(marker)
}
