odbc_api/handles/buffer.rs
1use std::{
2 cmp::min,
3 ptr::{null, null_mut},
4};
5
6/// Clamps a usize between `0` and `i16::MAX`.
7pub fn clamp_small_int(n: usize) -> i16 {
8 min(n, i16::MAX as usize) as i16
9}
10
11/// Returns a pointer suitable to be passed as an output buffer to ODBC functions. Most notably it
12/// will return NULL for empty buffers.
13pub fn mut_buf_ptr<T>(buffer: &mut [T]) -> *mut T {
14 if buffer.is_empty() {
15 null_mut()
16 } else {
17 buffer.as_mut_ptr()
18 }
19}
20
21/// Returns a pointer suitable to be passed as an output buffer to ODBC functions. Most notably it
22/// will return NULL for empty buffers.
23pub fn buf_ptr<T>(buffer: &[T]) -> *const T {
24 if buffer.is_empty() {
25 null()
26 } else {
27 buffer.as_ptr()
28 }
29}