Skip to content

Commit 0d5e799

Browse files
authored
Merge pull request #39 from rust-embedded-community/use-ffi-types
Remove the ctype module and use core::ffi.
2 parents 8bed81a + a12e1cb commit 0d5e799

23 files changed

Lines changed: 435 additions & 496 deletions

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ authors = ["Jonathan 'theJPster' Pallant <github@thejpster.org.uk>"]
55
edition = "2021"
66
description = "Tiny, incomplete C library for bare-metal targets, written in Stable (but Unsafe) Rust"
77
license-file = "LICENCES.md"
8+
rust-version = "1.77"
89
readme = "README.md"
910
repository = "https://github.com/rust-embedded-community/tinyrlibc"
1011

src/abs.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
//!
33
//! Licensed under the Blue Oak Model Licence 1.0.0
44
5-
use crate::CInt;
5+
use core::ffi::c_int;
66

77
/// Rust implementation of C library function `abs`
88
#[cfg_attr(feature = "abs", no_mangle)]
9-
pub extern "C" fn abs(i: CInt) -> CInt {
9+
pub extern "C" fn abs(i: c_int) -> c_int {
1010
i.abs()
1111
}
1212

src/ctype.rs

Lines changed: 0 additions & 73 deletions
This file was deleted.

src/itoa.rs

Lines changed: 36 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//! Copyright (c) Jonathan 'theJPster' Pallant 2019
77
//! Licensed under the Blue Oak Model Licence 1.0.0
88
9-
use crate::CChar;
9+
use core::ffi::c_char;
1010

1111
/// Formats the given value `i`, with the given radix, into the given buffer (of the given length).
1212
///
@@ -17,15 +17,15 @@ use crate::CChar;
1717
/// or -1 if the buffer wasn't large enough.
1818
#[cfg_attr(not(feature = "itoa"), export_name = "tinyrlibc_itoa")]
1919
#[cfg_attr(feature = "itoa", no_mangle)]
20-
pub unsafe extern "C" fn itoa(i: i64, s: *mut CChar, s_len: usize, radix: u8) -> i32 {
20+
pub unsafe extern "C" fn itoa(i: i64, s: *mut c_char, s_len: usize, radix: u8) -> i32 {
2121
let (is_negative, pos_i) = if i < 0 {
2222
(true, (-i) as u64)
2323
} else {
2424
(false, i as u64)
2525
};
2626

2727
if is_negative && (s_len > 0) {
28-
core::ptr::write(s, b'-');
28+
core::ptr::write(s, b'-' as c_char);
2929
utoa(pos_i, s.offset(1), s_len - 1, radix)
3030
} else {
3131
utoa(pos_i, s, s_len, radix)
@@ -41,17 +41,17 @@ pub unsafe extern "C" fn itoa(i: i64, s: *mut CChar, s_len: usize, radix: u8) ->
4141
/// or -1 if the buffer wasn't large enough.
4242
#[cfg_attr(not(feature = "utoa"), export_name = "tinyrlibc_utoa")]
4343
#[cfg_attr(feature = "utoa", no_mangle)]
44-
pub unsafe extern "C" fn utoa(mut u: u64, s: *mut CChar, s_len: usize, radix: u8) -> i32 {
44+
pub unsafe extern "C" fn utoa(mut u: u64, s: *mut c_char, s_len: usize, radix: u8) -> i32 {
4545
let buffer_slice = core::slice::from_raw_parts_mut(s, s_len);
4646

4747
// Build the number up in buffer s in reverse order
4848
let mut index = 0usize;
4949
for slot in buffer_slice.iter_mut() {
50-
let digit = (u % radix as u64) as u8;
50+
let digit = (u % radix as u64) as c_char;
5151
if digit <= 9 {
52-
*slot = digit + b'0';
52+
*slot = digit + (b'0' as c_char);
5353
} else {
54-
*slot = digit - 10 + b'a';
54+
*slot = digit - 10 + (b'a' as c_char);
5555
}
5656
index += 1;
5757
u /= radix as u64;
@@ -66,7 +66,7 @@ pub unsafe extern "C" fn utoa(mut u: u64, s: *mut CChar, s_len: usize, radix: u8
6666

6767
// Null-terminate
6868
if index < buffer_slice.len() {
69-
buffer_slice[index] = b'\0';
69+
buffer_slice[index] = 0;
7070
}
7171

7272
// Reverse buffer into correct order
@@ -82,155 +82,116 @@ mod test {
8282

8383
#[test]
8484
fn zero() {
85-
let mut buf = [b'\0'; 32];
85+
let mut buf = [0; 32];
8686
assert_eq!(unsafe { itoa(0, buf.as_mut_ptr(), buf.len(), 10) }, 1);
87-
assert_eq!(
88-
unsafe { strcmp(buf.as_ptr() as *const u8, b"0\0" as *const u8) },
89-
0
90-
);
87+
assert_eq!(unsafe { strcmp(buf.as_ptr(), c"0".as_ptr()) }, 0);
9188
}
9289

9390
#[test]
9491
fn one() {
95-
let mut buf = [b'\0'; 32];
92+
let mut buf = [0; 32];
9693
assert_eq!(unsafe { itoa(1, buf.as_mut_ptr(), buf.len(), 10) }, 1);
97-
assert_eq!(
98-
unsafe { strcmp(buf.as_ptr() as *const u8, b"1\0" as *const u8) },
99-
0
100-
);
94+
assert_eq!(unsafe { strcmp(buf.as_ptr(), c"1".as_ptr()) }, 0);
10195
}
10296

10397
#[test]
10498
fn hundredish() {
105-
let mut buf = [b'\0'; 32];
99+
let mut buf = [0; 32];
106100
assert_eq!(unsafe { itoa(123, buf.as_mut_ptr(), buf.len(), 10) }, 3);
107-
assert_eq!(
108-
unsafe { strcmp(buf.as_ptr() as *const u8, b"123\0" as *const u8) },
109-
0
110-
);
101+
assert_eq!(unsafe { strcmp(buf.as_ptr(), c"123".as_ptr()) }, 0);
111102
}
112103

113104
#[test]
114105
fn too_small() {
115-
let mut buf = [b'\0'; 1];
106+
let mut buf = [0; 1];
116107
assert_eq!(unsafe { itoa(10, buf.as_mut_ptr(), buf.len(), 10) }, -1);
117108
}
118109

119110
#[test]
120111
fn hex() {
121-
let mut buf = [b'\0'; 32];
112+
let mut buf = [0; 32];
122113
assert_eq!(
123114
unsafe { itoa(0xDEADBEEF, buf.as_mut_ptr(), buf.len(), 16) },
124115
8
125116
);
126-
assert_eq!(
127-
unsafe { strcmp(buf.as_ptr() as *const u8, b"deadbeef\0" as *const u8) },
128-
0
129-
);
117+
assert_eq!(unsafe { strcmp(buf.as_ptr(), c"deadbeef".as_ptr()) }, 0);
130118
}
131119

132120
#[test]
133121
fn octal() {
134-
let mut buf = [b'\0'; 32];
122+
let mut buf = [0; 32];
135123
assert_eq!(unsafe { itoa(0o774, buf.as_mut_ptr(), buf.len(), 8) }, 3);
136-
assert_eq!(
137-
unsafe { strcmp(buf.as_ptr() as *const u8, b"774\0" as *const u8) },
138-
0
139-
);
124+
assert_eq!(unsafe { strcmp(buf.as_ptr(), c"774".as_ptr()) }, 0);
140125
}
141126

142127
#[test]
143128
fn binary() {
144-
let mut buf = [b'\0'; 32];
129+
let mut buf = [0; 32];
145130
assert_eq!(
146131
unsafe { itoa(0b11100010, buf.as_mut_ptr(), buf.len(), 2) },
147132
8
148133
);
149-
assert_eq!(
150-
unsafe { strcmp(buf.as_ptr() as *const u8, b"11100010\0" as *const u8) },
151-
0
152-
);
134+
assert_eq!(unsafe { strcmp(buf.as_ptr(), c"11100010".as_ptr()) }, 0);
153135
}
154136

155137
#[test]
156138
fn negative() {
157-
let mut buf = [b'\0'; 32];
139+
let mut buf = [0; 32];
158140
unsafe { itoa(-123, buf.as_mut_ptr(), buf.len(), 10) };
159-
assert_eq!(
160-
unsafe { strcmp(buf.as_ptr() as *const u8, b"-123\0" as *const u8) },
161-
0
162-
);
141+
assert_eq!(unsafe { strcmp(buf.as_ptr(), c"-123".as_ptr()) }, 0);
163142
}
164143

165144
#[test]
166145
fn unsigned_zero() {
167-
let mut buf = [b'\0'; 32];
146+
let mut buf = [0; 32];
168147
assert_eq!(unsafe { utoa(0, buf.as_mut_ptr(), buf.len(), 10) }, 1);
169-
assert_eq!(
170-
unsafe { strcmp(buf.as_ptr() as *const u8, b"0\0" as *const u8) },
171-
0
172-
);
148+
assert_eq!(unsafe { strcmp(buf.as_ptr(), c"0".as_ptr()) }, 0);
173149
}
174150

175151
#[test]
176152
fn unsigned_one() {
177-
let mut buf = [b'\0'; 32];
153+
let mut buf = [0; 32];
178154
assert_eq!(unsafe { utoa(1, buf.as_mut_ptr(), buf.len(), 10) }, 1);
179-
assert_eq!(
180-
unsafe { strcmp(buf.as_ptr() as *const u8, b"1\0" as *const u8) },
181-
0
182-
);
155+
assert_eq!(unsafe { strcmp(buf.as_ptr(), c"1".as_ptr()) }, 0);
183156
}
184157

185158
#[test]
186159
fn unsigned_hundredish() {
187-
let mut buf = [b'\0'; 32];
160+
let mut buf = [0; 32];
188161
assert_eq!(unsafe { utoa(123, buf.as_mut_ptr(), buf.len(), 10) }, 3);
189-
assert_eq!(
190-
unsafe { strcmp(buf.as_ptr() as *const u8, b"123\0" as *const u8) },
191-
0
192-
);
162+
assert_eq!(unsafe { strcmp(buf.as_ptr(), c"123".as_ptr()) }, 0);
193163
}
194164

195165
#[test]
196166
fn unsigned_too_small() {
197-
let mut buf = [b'\0'; 1];
167+
let mut buf = [0; 1];
198168
assert_eq!(unsafe { utoa(10, buf.as_mut_ptr(), buf.len(), 10) }, -1);
199169
}
200170

201171
#[test]
202172
fn unsigned_hex() {
203-
let mut buf = [b'\0'; 32];
173+
let mut buf = [0; 32];
204174
assert_eq!(
205175
unsafe { utoa(0xDEADBEEF, buf.as_mut_ptr(), buf.len(), 16) },
206176
8
207177
);
208-
assert_eq!(
209-
unsafe { strcmp(buf.as_ptr() as *const u8, b"deadbeef\0" as *const u8) },
210-
0
211-
);
178+
assert_eq!(unsafe { strcmp(buf.as_ptr(), c"deadbeef".as_ptr()) }, 0);
212179
}
213180

214181
#[test]
215182
fn unsigned_octal() {
216-
let mut buf = [b'\0'; 32];
183+
let mut buf = [0; 32];
217184
assert_eq!(unsafe { utoa(0o774, buf.as_mut_ptr(), buf.len(), 8) }, 3);
218-
assert_eq!(
219-
unsafe { strcmp(buf.as_ptr() as *const u8, b"774\0" as *const u8) },
220-
0
221-
);
185+
assert_eq!(unsafe { strcmp(buf.as_ptr(), c"774".as_ptr()) }, 0);
222186
}
223187

224188
#[test]
225189
fn unsigned_binary() {
226-
let mut buf = [b'\0'; 32];
190+
let mut buf = [0; 32];
227191
assert_eq!(
228192
unsafe { utoa(0b11100010, buf.as_mut_ptr(), buf.len(), 2) },
229193
8
230194
);
231-
assert_eq!(
232-
unsafe { strcmp(buf.as_ptr() as *const u8, b"11100010\0" as *const u8) },
233-
0
234-
);
195+
assert_eq!(unsafe { strcmp(buf.as_ptr(), c"11100010".as_ptr()) }, 0);
235196
}
236197
}

src/lib.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@
1111
#![allow(clippy::missing_safety_doc)]
1212
#![allow(unused_imports)]
1313

14-
// Useful imports
15-
mod ctype;
16-
pub use self::ctype::*;
17-
1814
// Stateless implementations.
1915
// rustfmt will keep these in alphabetical order.
2016
mod abs;

0 commit comments

Comments
 (0)