odbc_api/parameter/
c_string.rs

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