Skip to main content

quack_rs/appender/
scalars.rs

1// SPDX-License-Identifier: MIT
2// Copyright 2026 Tom F. <https://github.com/tomtom215/>
3// My way of giving something small back to the open source community
4// and encouraging more Rust development!
5
6//! The fixed-width numeric `append_*` methods, `append_bool` through `append_u128`.
7
8use libduckdb_sys::{
9    duckdb_append_hugeint, duckdb_append_uhugeint, duckdb_hugeint, duckdb_uhugeint,
10};
11
12use super::{AppendError, Appender};
13
14/// Generates the fixed-width numeric `append_*` methods, which differ only in
15/// the C function they call.
16macro_rules! append_scalar {
17    ($($(#[$attr:meta])* $name:ident($ty:ty) => $c_fn:ident),* $(,)?) => {
18        impl Appender {
19            $(
20                $(#[$attr])*
21                ///
22                /// # Errors
23                ///
24                /// Returns an [`AppendError`] if the append fails.
25                pub fn $name(&self, value: $ty) -> Result<(), AppendError> {
26                    // SAFETY: self.handle is valid.
27                    self.append_one(|| unsafe { libduckdb_sys::$c_fn(self.handle, value) })
28                }
29            )*
30        }
31    };
32}
33
34append_scalar! {
35    /// Appends a `BOOLEAN`.
36    append_bool(bool) => duckdb_append_bool,
37    /// Appends a `TINYINT`.
38    append_i8(i8) => duckdb_append_int8,
39    /// Appends a `SMALLINT`.
40    append_i16(i16) => duckdb_append_int16,
41    /// Appends an `INTEGER`.
42    append_i32(i32) => duckdb_append_int32,
43    /// Appends a `BIGINT`.
44    append_i64(i64) => duckdb_append_int64,
45    /// Appends a `UTINYINT`.
46    append_u8(u8) => duckdb_append_uint8,
47    /// Appends a `USMALLINT`.
48    append_u16(u16) => duckdb_append_uint16,
49    /// Appends a `UINTEGER`.
50    append_u32(u32) => duckdb_append_uint32,
51    /// Appends a `UBIGINT`.
52    append_u64(u64) => duckdb_append_uint64,
53    /// Appends a `FLOAT`.
54    append_f32(f32) => duckdb_append_float,
55    /// Appends a `DOUBLE`.
56    append_f64(f64) => duckdb_append_double,
57}
58
59impl Appender {
60    /// Appends a `HUGEINT`.
61    ///
62    /// # Errors
63    ///
64    /// Returns an [`AppendError`] if the append fails.
65    pub fn append_i128(&self, value: i128) -> Result<(), AppendError> {
66        let raw = duckdb_hugeint {
67            #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
68            lower: value as u64,
69            #[allow(clippy::cast_possible_truncation)]
70            upper: (value >> 64) as i64,
71        };
72        // SAFETY: self.handle is valid.
73        self.append_one(|| unsafe { duckdb_append_hugeint(self.handle, raw) })
74    }
75
76    /// Appends a `UHUGEINT`.
77    ///
78    /// # Errors
79    ///
80    /// Returns an [`AppendError`] if the append fails.
81    pub fn append_u128(&self, value: u128) -> Result<(), AppendError> {
82        let raw = duckdb_uhugeint {
83            #[allow(clippy::cast_possible_truncation)]
84            lower: value as u64,
85            #[allow(clippy::cast_possible_truncation)]
86            upper: (value >> 64) as u64,
87        };
88        // SAFETY: self.handle is valid.
89        self.append_one(|| unsafe { duckdb_append_uhugeint(self.handle, raw) })
90    }
91}