fixed/
borshize.rs

1// Copyright © 2018–2025 Trevor Spiteri
2
3// This library is free software: you can redistribute it and/or
4// modify it under the terms of either
5//
6//   * the Apache License, Version 2.0 or
7//   * the MIT License
8//
9// at your option.
10//
11// You should have recieved copies of the Apache License and the MIT
12// License along with the library. If not, see
13// <https://www.apache.org/licenses/LICENSE-2.0> and
14// <https://opensource.org/licenses/MIT>.
15
16use crate::{
17    FixedI8, FixedI16, FixedI32, FixedI64, FixedI128, FixedU8, FixedU16, FixedU32, FixedU64,
18    FixedU128,
19};
20use borsh::io::{Read, Result, Write};
21use borsh::{BorshDeserialize, BorshSerialize};
22
23macro_rules! borsh_fixed {
24    ($Fixed:ident is $TBits:ident) => {
25        impl<Frac> BorshSerialize for $Fixed<Frac> {
26            #[inline]
27            fn serialize<W: Write>(&self, writer: &mut W) -> Result<()> {
28                <$TBits as BorshSerialize>::serialize(&self.bits, writer)
29            }
30        }
31
32        impl<Frac> BorshDeserialize for $Fixed<Frac> {
33            #[inline]
34            fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
35                <$TBits as BorshDeserialize>::deserialize_reader(reader).map($Fixed::from_bits)
36            }
37        }
38    };
39}
40
41borsh_fixed! { FixedI8 is i8 }
42borsh_fixed! { FixedI16 is i16 }
43borsh_fixed! { FixedI32 is i32 }
44borsh_fixed! { FixedI64 is i64 }
45borsh_fixed! { FixedI128 is i128 }
46borsh_fixed! { FixedU8 is u8 }
47borsh_fixed! { FixedU16 is u16 }
48borsh_fixed! { FixedU32 is u32 }
49borsh_fixed! { FixedU64 is u64 }
50borsh_fixed! { FixedU128 is u128 }