odbc_api/parameter/
c_string.rs

1//! Module contains trait implementations for `CStr` and `CString`.
2
3use std::{
4    ffi::{CStr, CString, c_void},
5    num::NonZeroUsize,
6};
7
8use odbc_sys::{CDataType, NTS};
9
10use crate::{
11    DataType,
12    handles::{CData, HasDataType},
13};
14
15use super::CElement;
16
17unsafe impl CData for CStr {
18    fn cdata_type(&self) -> CDataType {
19        CDataType::Char
20    }
21
22    fn indicator_ptr(&self) -> *const isize {
23        &NTS as *const isize
24    }
25
26    fn value_ptr(&self) -> *const c_void {
27        self.as_ptr() as *const c_void
28    }
29
30    fn buffer_length(&self) -> isize {
31        0
32    }
33}
34
35impl HasDataType for CStr {
36    fn data_type(&self) -> DataType {
37        DataType::Varchar {
38            length: NonZeroUsize::new(self.to_bytes().len()),
39        }
40    }
41}
42unsafe impl CElement for CStr {
43    /// `CStr` is terminated by zero. Indicator can therefore never indicate a truncated value and
44    /// is always complete.
45    fn assert_completness(&self) {}
46}
47
48unsafe impl CData for CString {
49    fn cdata_type(&self) -> CDataType {
50        CDataType::Char
51    }
52
53    fn indicator_ptr(&self) -> *const isize {
54        &NTS as *const isize
55    }
56
57    fn value_ptr(&self) -> *const c_void {
58        self.as_ptr() as *const c_void
59    }
60
61    fn buffer_length(&self) -> isize {
62        0
63    }
64}
65
66impl HasDataType for CString {
67    fn data_type(&self) -> DataType {
68        DataType::Varchar {
69            length: NonZeroUsize::new(self.as_bytes().len()),
70        }
71    }
72}
73
74unsafe impl CElement for CString {
75    /// `CString`` is terminated by zero. Indicator can therefore never indicate a truncated value
76    /// and is always complete.
77    fn assert_completness(&self) {}
78}