Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
## Unreleased

* [#7] - Add `strtoul` and `strcpy`
* [#8] - Add `abs`

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

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

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ This crate basically came about so that the [nrfxlib](https://github.com/NordicP

## Implemented so far

* abs
* strol
* atoi
* strcmp
Expand Down
36 changes: 36 additions & 0 deletions src/abs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//! Rust implementation of C library function `abs`
//!
//! Licensed under the Blue Oak Model Licence 1.0.0

use crate::CInt;

/// Calculates the integer absolute value
///
/// ```
/// use tinyrlibc::abs;
/// assert_eq!(abs(-2), 2);
/// ```
#[no_mangle]
pub extern "C" fn abs(i: CInt) -> CInt {
i.abs()
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn neg() {
assert_eq!(abs(-2), 2);
}

#[test]
fn pos() {
assert_eq!(abs(3), 3);
}

#[test]
fn zero() {
assert_eq!(abs(0), 0);
}
}
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
#[allow(unused_imports)]
use std as core;

mod abs;
pub use self::abs::abs;

mod strcmp;
pub use self::strcmp::strcmp;

Expand Down