mz_pgrepr/value/
unsigned.rs

1// Copyright Materialize, Inc. and contributors. All rights reserved.
2//
3// Use of this software is governed by the Business Source License
4// included in the LICENSE file.
5//
6// As of the Change Date specified in that file, in accordance with
7// the Business Source License, use of this software will be governed
8// by the Apache License, Version 2.0.
9
10use std::error::Error;
11
12use bytes::{BufMut, BytesMut};
13use postgres_types::{FromSql, IsNull, ToSql, Type, to_sql_checked};
14
15use crate::oid;
16
17/// A wrapper for 16-bit unsigned integers that can be serialized to and
18/// deserialized from the PostgreSQL binary format.
19#[derive(Debug, Clone, Copy)]
20pub struct UInt2(pub u16);
21
22impl<'a> FromSql<'a> for UInt2 {
23    fn from_sql(_: &Type, raw: &'a [u8]) -> Result<UInt2, Box<dyn Error + Sync + Send>> {
24        Ok(UInt2(u16::from_be_bytes(raw.try_into()?)))
25    }
26
27    fn accepts(ty: &Type) -> bool {
28        ty.oid() == oid::TYPE_UINT2_OID
29    }
30}
31
32impl ToSql for UInt2 {
33    fn to_sql(
34        &self,
35        _: &Type,
36        out: &mut BytesMut,
37    ) -> Result<IsNull, Box<dyn Error + 'static + Send + Sync>> {
38        out.put_u16(self.0);
39        Ok(IsNull::No)
40    }
41
42    fn accepts(ty: &Type) -> bool {
43        ty.oid() == oid::TYPE_UINT2_OID
44    }
45
46    to_sql_checked!();
47}
48
49/// A wrapper for 32-bit unsigned integers that can be serialized to and
50/// deserialized from the PostgreSQL binary format.
51#[derive(Debug, Clone, Copy)]
52pub struct UInt4(pub u32);
53
54impl<'a> FromSql<'a> for UInt4 {
55    fn from_sql(_: &Type, raw: &'a [u8]) -> Result<UInt4, Box<dyn Error + Sync + Send>> {
56        Ok(UInt4(u32::from_be_bytes(raw.try_into()?)))
57    }
58
59    fn accepts(ty: &Type) -> bool {
60        ty.oid() == oid::TYPE_UINT4_OID
61    }
62}
63
64impl ToSql for UInt4 {
65    fn to_sql(
66        &self,
67        _: &Type,
68        out: &mut BytesMut,
69    ) -> Result<IsNull, Box<dyn Error + 'static + Send + Sync>> {
70        out.put_u32(self.0);
71        Ok(IsNull::No)
72    }
73
74    fn accepts(ty: &Type) -> bool {
75        ty.oid() == oid::TYPE_UINT4_OID
76    }
77
78    to_sql_checked!();
79}
80
81/// A wrapper for 64-bit unsigned integers that can be serialized to and
82/// deserialized from the PostgreSQL binary format.
83#[derive(Debug, Clone, Copy)]
84pub struct UInt8(pub u64);
85
86impl<'a> FromSql<'a> for UInt8 {
87    fn from_sql(_: &Type, raw: &'a [u8]) -> Result<UInt8, Box<dyn Error + Sync + Send>> {
88        Ok(UInt8(u64::from_be_bytes(raw.try_into()?)))
89    }
90
91    fn accepts(ty: &Type) -> bool {
92        ty.oid() == oid::TYPE_UINT8_OID
93    }
94}
95
96impl ToSql for UInt8 {
97    fn to_sql(
98        &self,
99        _: &Type,
100        out: &mut BytesMut,
101    ) -> Result<IsNull, Box<dyn Error + 'static + Send + Sync>> {
102        out.put_u64(self.0);
103        Ok(IsNull::No)
104    }
105
106    fn accepts(ty: &Type) -> bool {
107        ty.oid() == oid::TYPE_UINT8_OID
108    }
109
110    to_sql_checked!();
111}