Read an f64 from the value at `offset`. Handles float32, float64, and all integer types (coerced to f64).
(buf: &[u8], offset: usize)
| 214 | /// Read an f64 from the value at `offset`. Handles float32, float64, |
| 215 | /// and all integer types (coerced to f64). |
| 216 | pub fn read_f64(buf: &[u8], offset: usize) -> Option<f64> { |
| 217 | let tag = get(buf, offset)?; |
| 218 | match tag { |
| 219 | // positive fixint |
| 220 | 0x00..=0x7f => Some(tag as f64), |
| 221 | // negative fixint |
| 222 | 0xe0..=0xff => Some((tag as i8) as f64), |
| 223 | FLOAT64 => { |
| 224 | let bits = read_u64_be(buf, offset + 1)?; |
| 225 | Some(f64::from_bits(bits)) |
| 226 | } |
| 227 | FLOAT32 => { |
| 228 | let bits = read_u32_be(buf, offset + 1)?; |
| 229 | Some(f32::from_bits(bits) as f64) |
| 230 | } |
| 231 | UINT8 => Some(get(buf, offset + 1)? as f64), |
| 232 | UINT16 => Some(read_u16_be(buf, offset + 1)? as f64), |
| 233 | UINT32 => Some(read_u32_be(buf, offset + 1)? as f64), |
| 234 | UINT64 => Some(read_u64_be(buf, offset + 1)? as f64), |
| 235 | INT8 => Some(get(buf, offset + 1)? as i8 as f64), |
| 236 | INT16 => Some(read_u16_be(buf, offset + 1)? as i16 as f64), |
| 237 | INT32 => Some(read_u32_be(buf, offset + 1)? as i32 as f64), |
| 238 | INT64 => Some(read_u64_be(buf, offset + 1)? as i64 as f64), |
| 239 | _ => None, |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | /// Read an i64 from the value at `offset`. Handles all integer types. |
| 244 | /// Floats return `None` — use `read_f64` for those. |