summaryrefslogtreecommitdiff
path: root/rust/strprintf/src/traits.rs
blob: 5acf22bb44b80e003335dc7399703c3b12d7be91 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
//! Traits required by `fmt!` macro.
//!
//! `fmt!` needs to convert values from Rust types like `u64` and `String` to C types like
//! `uint64_t` and `const char*`.
//!
//! For integer types, this happens automatically, e.g. `u64` can be passed directly into
//! `libc::snprintf`.
//!
//! Floating-point types are a bit more complicated: `f32` (C's `float`) has to be converted to
//! `f64` (C's `double`) because that's how variadic FFI works. `f64` can be passed over FFI
//! without any problems.
//!
//! Strings are the most complicated of all: Rust represents them as an array of bytes, whereas
//! C represents them as a chunk of memory terminated by a null byte. As a result, converting
//! a `String` into `const char*` requires an allocation to temporarily store a string in
//! a C-compatible fashion. `snprintf` doesn't understand Rust's borrowing, so we also need a Rust
//! object to hold that memory while `snprintf` works with a pointer to it.
//!
//! To support all of this, `fmt!` uses a two-stage process:
//! 1. convert a Rust type into an intermediate "holder" that owns a C-compatible representation of
//!    that value (`Printfable` trait);
//! 2. borrow the C-compatible representation of the value from the "holder" (`CReprHolder` trait).
//!
//! For example, a "holder" for a `String` is `std::ffi::CString`. It can be borrowed to call its
//! `to_ptr` method that returns `*const libc::c_char`.
//!
//! For numeric types, the holder is the same as the type itself, and the values are simply copied.
//! For example, `u64`'s "holder" is `u64`, which is then borrowed to get a `u64` again.

use std::ffi::CString;

/// Trait of all types that can be formatted with `fmt!` macro.
///
/// See module-level documentation for an explanation of this.
pub trait Printfable {
    /// A "holder" type for this type.
    type Holder: CReprHolder;

    /// Try to convert the value into "holder" type.
    ///
    /// This can fail for some types, e.g. `String` might fail to convert to `CString` if it
    /// contains null bytes.
    fn into_c_repr_holder(self) -> Option<<Self as Printfable>::Holder>;
}

impl Printfable for i32 {
    type Holder = i32;
    fn into_c_repr_holder(self) -> Option<<Self as Printfable>::Holder> {
        Some(self)
    }
}

impl Printfable for u32 {
    type Holder = u32;
    fn into_c_repr_holder(self) -> Option<<Self as Printfable>::Holder> {
        Some(self)
    }
}

impl Printfable for i64 {
    type Holder = i64;
    fn into_c_repr_holder(self) -> Option<<Self as Printfable>::Holder> {
        Some(self)
    }
}

impl Printfable for u64 {
    type Holder = u64;
    fn into_c_repr_holder(self) -> Option<<Self as Printfable>::Holder> {
        Some(self)
    }
}

/// `f32` can't be passed to variadic functions like `snprintf`, so it's converted into `f64`
impl Printfable for f32 {
    type Holder = f64;
    fn into_c_repr_holder(self) -> Option<<Self as Printfable>::Holder> {
        Some(self as f64)
    }
}

impl Printfable for f64 {
    type Holder = f64;
    fn into_c_repr_holder(self) -> Option<<Self as Printfable>::Holder> {
        Some(self)
    }
}

impl<'a> Printfable for &'a str {
    type Holder = CString;
    fn into_c_repr_holder(self) -> Option<<Self as Printfable>::Holder> {
        CString::new(self).ok()
    }
}

impl<'a> Printfable for &'a String {
    type Holder = CString;
    fn into_c_repr_holder(self) -> Option<<Self as Printfable>::Holder> {
        CString::new(self.as_str()).ok()
    }
}

impl Printfable for String {
    type Holder = CString;
    fn into_c_repr_holder(self) -> Option<<Self as Printfable>::Holder> {
        CString::new(self).ok()
    }
}

impl<T> Printfable for *const T {
    type Holder = *const libc::c_void;
    fn into_c_repr_holder(self) -> Option<<Self as Printfable>::Holder> {
        Some(self as *const libc::c_void)
    }
}

/// Trait of all types that hold a C representation of a value.
///
/// See module-level documentation for an explanation of this.
pub trait CReprHolder {
    type Output;
    fn to_c_repr(&self) -> <Self as CReprHolder>::Output;
}

impl CReprHolder for i32 {
    type Output = i32;
    fn to_c_repr(&self) -> <Self as CReprHolder>::Output {
        *self
    }
}

impl CReprHolder for u32 {
    type Output = u32;
    fn to_c_repr(&self) -> <Self as CReprHolder>::Output {
        *self
    }
}

impl CReprHolder for i64 {
    type Output = i64;
    fn to_c_repr(&self) -> <Self as CReprHolder>::Output {
        *self
    }
}

impl CReprHolder for u64 {
    type Output = u64;
    fn to_c_repr(&self) -> <Self as CReprHolder>::Output {
        *self
    }
}

// No need to implement CReprHolder for f32, as this trait is only invoked on results of
// `Printfable::into_c_repr_holder`, and that converts f32 to f64

impl CReprHolder for f64 {
    type Output = f64;
    fn to_c_repr(&self) -> <Self as CReprHolder>::Output {
        *self
    }
}

impl CReprHolder for CString {
    type Output = *const libc::c_char;
    fn to_c_repr(&self) -> <Self as CReprHolder>::Output {
        self.as_ptr()
    }
}

impl CReprHolder for *const libc::c_void {
    type Output = *const libc::c_void;
    fn to_c_repr(&self) -> <Self as CReprHolder>::Output {
        *self
    }
}