Skip to content

Commit 38a3bcd

Browse files
authored
Add abs (#8)
* add abs * abs: rustfmt * abs: remove unsafe
1 parent d356185 commit 38a3bcd

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
## Unreleased
44

55
* [#7] - Add `strtoul` and `strcpy`
6+
* [#8] - Add `abs`
67

78
[#7]: https://github.com/rust-embedded-community/tinyrlibc/pull/7
9+
[#8]: https://github.com/rust-embedded-community/tinyrlibc/pull/8
810

911
## v0.2.2 (2022-03-17)
1012

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ This crate basically came about so that the [nrfxlib](https://github.com/NordicP
88

99
## Implemented so far
1010

11+
* abs
1112
* strol
1213
* atoi
1314
* strcmp

src/abs.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//! Rust implementation of C library function `abs`
2+
//!
3+
//! Licensed under the Blue Oak Model Licence 1.0.0
4+
5+
use crate::CInt;
6+
7+
/// Calculates the integer absolute value
8+
///
9+
/// ```
10+
/// use tinyrlibc::abs;
11+
/// assert_eq!(abs(-2), 2);
12+
/// ```
13+
#[no_mangle]
14+
pub extern "C" fn abs(i: CInt) -> CInt {
15+
i.abs()
16+
}
17+
18+
#[cfg(test)]
19+
mod test {
20+
use super::*;
21+
22+
#[test]
23+
fn neg() {
24+
assert_eq!(abs(-2), 2);
25+
}
26+
27+
#[test]
28+
fn pos() {
29+
assert_eq!(abs(3), 3);
30+
}
31+
32+
#[test]
33+
fn zero() {
34+
assert_eq!(abs(0), 0);
35+
}
36+
}

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
#[allow(unused_imports)]
1414
use std as core;
1515

16+
mod abs;
17+
pub use self::abs::abs;
18+
1619
mod strcmp;
1720
pub use self::strcmp::strcmp;
1821

0 commit comments

Comments
 (0)