udf_sys/
lib.rs

1//! Bindings to C for SQL UDF-related types
2//!
3//! Types in this module were autogenerated. Documentation mostly comes from the
4//! C header file, but some clarifications were added. Some mut -> const changes
5//! were done as makes sense.
6//!
7//! To regenerate this file, run:
8//!
9//! ```sh
10//! bindgen udf_registration_types.c \
11//!     --default-enum-style=rust_non_exhaustive \
12//!     --no-derive-copy
13//! ```
14//!
15//! _You're off the edge of the map, mate. Here there be monsters!_
16
17/* automatically generated by rust-bindgen 0.60.1 */
18
19#![allow(non_camel_case_types)]
20#![allow(non_upper_case_globals)]
21#![allow(non_snake_case)]
22
23/// C builtin
24pub const true_: u32 = 1;
25
26/// C builtin
27pub const false_: u32 = 0;
28
29/// C builtin
30pub const __bool_true_false_are_defined: u32 = 1;
31
32/// Type of the user defined function return slot and arguments
33// This is `repr(C)` to ensure it is represented the same as C enums.
34#[repr(C)]
35#[non_exhaustive]
36#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
37pub enum Item_result {
38    /// Invalid value (not valid for UDFs)
39    INVALID_RESULT = -1,
40
41    /// Value representing a string (char *)
42    STRING_RESULT = 0,
43
44    /// Value representing a real (double)
45    REAL_RESULT = 1,
46
47    /// Value representing an int (long long)
48    INT_RESULT = 2,
49
50    /// Value representing a row (not valid for UDFs)
51    ROW_RESULT = 3,
52
53    /// Value representing a decimal (char *)
54    DECIMAL_RESULT = 4,
55}
56
57impl TryFrom<i32> for Item_result {
58    type Error = String;
59
60    fn try_from(value: i32) -> Result<Self, Self::Error> {
61        match value {
62            x if x == Self::INVALID_RESULT as i32 => Ok(Self::INVALID_RESULT),
63            x if x == Self::STRING_RESULT as i32 => Ok(Self::STRING_RESULT),
64            x if x == Self::REAL_RESULT as i32 => Ok(Self::REAL_RESULT),
65            x if x == Self::INT_RESULT as i32 => Ok(Self::INT_RESULT),
66            x if x == Self::ROW_RESULT as i32 => Ok(Self::ROW_RESULT),
67            x if x == Self::DECIMAL_RESULT as i32 => Ok(Self::DECIMAL_RESULT),
68            _ => Err(format!("invalid arg type {value} received")),
69        }
70    }
71}
72
73/// Representation of a sequence of SQL arguments
74#[repr(C)]
75#[derive(Debug, Clone)]
76pub struct UDF_ARGS {
77    /// Number of arguments present
78    pub arg_count: ::std::ffi::c_uint,
79
80    /// Buffer of `item_result` pointers that indicate argument type
81    ///
82    /// Remains mutable because it can be set in `xxx_init`
83    pub arg_types: *mut Item_result,
84
85    /// Buffer of pointers to the arguments. Arguments may be of any type
86    /// (specified in `arg_type`).
87    pub args: *const *const ::std::ffi::c_char,
88
89    /// Buffer of lengths for string arguments
90    pub lengths: *const ::std::ffi::c_ulong,
91
92    /// Indicates whether the argument may be null or not
93    pub maybe_null: *const ::std::ffi::c_char,
94
95    /// Buffer of string pointers that hold variable names, for use with error
96    /// messages
97    pub attributes: *const *const ::std::ffi::c_char,
98
99    /// Buffer of lengths of attributes
100    pub attribute_lengths: *const ::std::ffi::c_ulong,
101
102    /// Extension is currently unused
103    pub extension: *const ::std::ffi::c_void,
104}
105
106/// Information about the result of a user defined function
107#[repr(C)]
108#[derive(Debug, Clone)]
109pub struct UDF_INIT {
110    /// True if the function can return NULL
111    pub maybe_null: bool,
112
113    /// This is used for real-returning functions
114    pub decimals: ::std::ffi::c_uint,
115
116    /// This is used for string functions
117    pub max_length: ::std::ffi::c_ulong,
118
119    /// free pointer for function data
120    pub ptr: *mut ::std::ffi::c_char,
121
122    /// True if function always returns the same value
123    pub const_item: bool,
124
125    /// Unused at this time
126    pub extension: *mut ::std::ffi::c_void,
127}
128
129/// A UDF function type indicator, currently unused
130#[repr(u32)]
131#[non_exhaustive]
132#[derive(Debug, Clone, Hash, PartialEq, Eq)]
133pub enum Item_udftype {
134    UDFTYPE_FUNCTION = 1,
135    UDFTYPE_AGGREGATE = 2,
136}
137
138/// Function signature of an `xxx_init(...)` function
139pub type Udf_func_init = Option<
140    unsafe extern "C" fn(
141        initid: *mut UDF_INIT,
142        args: *mut UDF_ARGS,
143        message: *mut ::std::ffi::c_char,
144    ) -> bool,
145>;
146
147/// Function signature of an `xxx_deinit(...)` function
148pub type Udf_func_deinit = Option<unsafe extern "C" fn(arg1: *mut UDF_INIT)>;
149
150/// Function signature of an `xxx_add(...)` aggregate function
151pub type Udf_func_add = Option<
152    unsafe extern "C" fn(
153        initid: *mut UDF_INIT,
154        args: *const UDF_ARGS,
155        is_null: *mut ::std::ffi::c_uchar,
156        error: *mut ::std::ffi::c_uchar,
157    ),
158>;
159
160/// Function signature of an `xxx_clear(...)` aggregate function
161pub type Udf_func_clear = Option<
162    unsafe extern "C" fn(
163        initid: *mut UDF_INIT,
164        is_null: *mut ::std::ffi::c_uchar,
165        error: *mut ::std::ffi::c_uchar,
166    ),
167>;
168
169/// Function signature of an `xxx(...)` function returning a SQL real
170pub type Udf_func_double = Option<
171    unsafe extern "C" fn(
172        initid: *mut UDF_INIT,
173        args: *const UDF_ARGS,
174        is_null: *mut ::std::ffi::c_uchar,
175        error: *mut ::std::ffi::c_uchar,
176    ) -> ::std::ffi::c_double,
177>;
178
179/// Function signature of an `xxx(...)` function returning a SQL int
180pub type Udf_func_longlong = Option<
181    unsafe extern "C" fn(
182        initid: *mut UDF_INIT,
183        args: *const UDF_ARGS,
184        is_null: *mut ::std::ffi::c_uchar,
185        error: *mut ::std::ffi::c_uchar,
186    ) -> ::std::ffi::c_longlong,
187>;
188
189/// Function signature of an `xxx(...)` function returning a SQL string
190pub type Udf_func_string = Option<
191    unsafe extern "C" fn(
192        initid: *mut UDF_INIT,
193        args: *const UDF_ARGS,
194        result: *mut ::std::ffi::c_char,
195        length: *mut ::std::ffi::c_ulong,
196        is_null: *mut ::std::ffi::c_uchar,
197        error: *mut ::std::ffi::c_uchar,
198    ) -> *mut ::std::ffi::c_char,
199>;
200
201/// Function signature of a void functin (unused)
202pub type Udf_func_any = Option<unsafe extern "C" fn()>;
203
204#[cfg(test)]
205mod tests {
206
207    use super::*;
208
209    #[test]
210    fn bindgen_test_layout_UDF_ARGS() {
211        assert_eq!(
212            ::std::mem::size_of::<UDF_ARGS>(),
213            64usize,
214            concat!("Size of: ", stringify!(UDF_ARGS))
215        );
216        assert_eq!(
217            ::std::mem::align_of::<UDF_ARGS>(),
218            8usize,
219            concat!("Alignment of ", stringify!(UDF_ARGS))
220        );
221        fn test_field_arg_count() {
222            assert_eq!(
223                unsafe {
224                    let uninit = ::std::mem::MaybeUninit::<UDF_ARGS>::uninit();
225                    let ptr = uninit.as_ptr();
226                    ::std::ptr::addr_of!((*ptr).arg_count) as usize - ptr as usize
227                },
228                0usize,
229                concat!(
230                    "Offset of field: ",
231                    stringify!(UDF_ARGS),
232                    "::",
233                    stringify!(arg_count)
234                )
235            );
236        }
237        test_field_arg_count();
238        fn test_field_arg_type() {
239            assert_eq!(
240                unsafe {
241                    let uninit = ::std::mem::MaybeUninit::<UDF_ARGS>::uninit();
242                    let ptr = uninit.as_ptr();
243                    ::std::ptr::addr_of!((*ptr).arg_types) as usize - ptr as usize
244                },
245                8usize,
246                concat!(
247                    "Offset of field: ",
248                    stringify!(UDF_ARGS),
249                    "::",
250                    stringify!(arg_type)
251                )
252            );
253        }
254        test_field_arg_type();
255        fn test_field_args() {
256            assert_eq!(
257                unsafe {
258                    let uninit = ::std::mem::MaybeUninit::<UDF_ARGS>::uninit();
259                    let ptr = uninit.as_ptr();
260                    ::std::ptr::addr_of!((*ptr).args) as usize - ptr as usize
261                },
262                16usize,
263                concat!(
264                    "Offset of field: ",
265                    stringify!(UDF_ARGS),
266                    "::",
267                    stringify!(args)
268                )
269            );
270        }
271        test_field_args();
272        fn test_field_lengths() {
273            assert_eq!(
274                unsafe {
275                    let uninit = ::std::mem::MaybeUninit::<UDF_ARGS>::uninit();
276                    let ptr = uninit.as_ptr();
277                    ::std::ptr::addr_of!((*ptr).lengths) as usize - ptr as usize
278                },
279                24usize,
280                concat!(
281                    "Offset of field: ",
282                    stringify!(UDF_ARGS),
283                    "::",
284                    stringify!(lengths)
285                )
286            );
287        }
288        test_field_lengths();
289        fn test_field_maybe_null() {
290            assert_eq!(
291                unsafe {
292                    let uninit = ::std::mem::MaybeUninit::<UDF_ARGS>::uninit();
293                    let ptr = uninit.as_ptr();
294                    ::std::ptr::addr_of!((*ptr).maybe_null) as usize - ptr as usize
295                },
296                32usize,
297                concat!(
298                    "Offset of field: ",
299                    stringify!(UDF_ARGS),
300                    "::",
301                    stringify!(maybe_null)
302                )
303            );
304        }
305        test_field_maybe_null();
306        fn test_field_attributes() {
307            assert_eq!(
308                unsafe {
309                    let uninit = ::std::mem::MaybeUninit::<UDF_ARGS>::uninit();
310                    let ptr = uninit.as_ptr();
311                    ::std::ptr::addr_of!((*ptr).attributes) as usize - ptr as usize
312                },
313                40usize,
314                concat!(
315                    "Offset of field: ",
316                    stringify!(UDF_ARGS),
317                    "::",
318                    stringify!(attributes)
319                )
320            );
321        }
322        test_field_attributes();
323        fn test_field_attribute_lengths() {
324            assert_eq!(
325                unsafe {
326                    let uninit = ::std::mem::MaybeUninit::<UDF_ARGS>::uninit();
327                    let ptr = uninit.as_ptr();
328                    ::std::ptr::addr_of!((*ptr).attribute_lengths) as usize - ptr as usize
329                },
330                48usize,
331                concat!(
332                    "Offset of field: ",
333                    stringify!(UDF_ARGS),
334                    "::",
335                    stringify!(attribute_lengths)
336                )
337            );
338        }
339        test_field_attribute_lengths();
340        fn test_field_extension() {
341            assert_eq!(
342                unsafe {
343                    let uninit = ::std::mem::MaybeUninit::<UDF_ARGS>::uninit();
344                    let ptr = uninit.as_ptr();
345                    ::std::ptr::addr_of!((*ptr).extension) as usize - ptr as usize
346                },
347                56usize,
348                concat!(
349                    "Offset of field: ",
350                    stringify!(UDF_ARGS),
351                    "::",
352                    stringify!(extension)
353                )
354            );
355        }
356        test_field_extension();
357    }
358
359    #[test]
360    fn bindgen_test_layout_UDF_INIT() {
361        assert_eq!(
362            ::std::mem::size_of::<UDF_INIT>(),
363            40usize,
364            concat!("Size of: ", stringify!(UDF_INIT))
365        );
366        assert_eq!(
367            ::std::mem::align_of::<UDF_INIT>(),
368            8usize,
369            concat!("Alignment of ", stringify!(UDF_INIT))
370        );
371        fn test_field_maybe_null() {
372            assert_eq!(
373                unsafe {
374                    let uninit = ::std::mem::MaybeUninit::<UDF_INIT>::uninit();
375                    let ptr = uninit.as_ptr();
376                    ::std::ptr::addr_of!((*ptr).maybe_null) as usize - ptr as usize
377                },
378                0usize,
379                concat!(
380                    "Offset of field: ",
381                    stringify!(UDF_INIT),
382                    "::",
383                    stringify!(maybe_null)
384                )
385            );
386        }
387        test_field_maybe_null();
388        fn test_field_decimals() {
389            assert_eq!(
390                unsafe {
391                    let uninit = ::std::mem::MaybeUninit::<UDF_INIT>::uninit();
392                    let ptr = uninit.as_ptr();
393                    ::std::ptr::addr_of!((*ptr).decimals) as usize - ptr as usize
394                },
395                4usize,
396                concat!(
397                    "Offset of field: ",
398                    stringify!(UDF_INIT),
399                    "::",
400                    stringify!(decimals)
401                )
402            );
403        }
404        test_field_decimals();
405        fn test_field_max_length() {
406            assert_eq!(
407                unsafe {
408                    let uninit = ::std::mem::MaybeUninit::<UDF_INIT>::uninit();
409                    let ptr = uninit.as_ptr();
410                    ::std::ptr::addr_of!((*ptr).max_length) as usize - ptr as usize
411                },
412                8usize,
413                concat!(
414                    "Offset of field: ",
415                    stringify!(UDF_INIT),
416                    "::",
417                    stringify!(max_length)
418                )
419            );
420        }
421        test_field_max_length();
422        fn test_field_ptr() {
423            assert_eq!(
424                unsafe {
425                    let uninit = ::std::mem::MaybeUninit::<UDF_INIT>::uninit();
426                    let ptr = uninit.as_ptr();
427                    ::std::ptr::addr_of!((*ptr).ptr) as usize - ptr as usize
428                },
429                16usize,
430                concat!(
431                    "Offset of field: ",
432                    stringify!(UDF_INIT),
433                    "::",
434                    stringify!(ptr)
435                )
436            );
437        }
438        test_field_ptr();
439        fn test_field_const_item() {
440            assert_eq!(
441                unsafe {
442                    let uninit = ::std::mem::MaybeUninit::<UDF_INIT>::uninit();
443                    let ptr = uninit.as_ptr();
444                    ::std::ptr::addr_of!((*ptr).const_item) as usize - ptr as usize
445                },
446                24usize,
447                concat!(
448                    "Offset of field: ",
449                    stringify!(UDF_INIT),
450                    "::",
451                    stringify!(const_item)
452                )
453            );
454        }
455        test_field_const_item();
456        fn test_field_extension() {
457            assert_eq!(
458                unsafe {
459                    let uninit = ::std::mem::MaybeUninit::<UDF_INIT>::uninit();
460                    let ptr = uninit.as_ptr();
461                    ::std::ptr::addr_of!((*ptr).extension) as usize - ptr as usize
462                },
463                32usize,
464                concat!(
465                    "Offset of field: ",
466                    stringify!(UDF_INIT),
467                    "::",
468                    stringify!(extension)
469                )
470            );
471        }
472        test_field_extension();
473    }
474}