From 4d74d2bd733279f38aa2cd1cccf93447737c25f7 Mon Sep 17 00:00:00 2001 From: sander Date: Fri, 19 Aug 2022 15:02:34 +0200 Subject: [PATCH 1/3] add abs --- CHANGELOG.md | 2 ++ README.md | 1 + src/abs.rs | 45 +++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 3 +++ 4 files changed, 51 insertions(+) create mode 100644 src/abs.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index d33332a..aa9e740 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/README.md b/README.md index b59d57c..e834105 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,7 @@ This crate basically came about so that the [nrfxlib](https://github.com/NordicP ## Implemented so far +* abs * strol * atoi * strcmp diff --git a/src/abs.rs b/src/abs.rs new file mode 100644 index 0000000..b85671b --- /dev/null +++ b/src/abs.rs @@ -0,0 +1,45 @@ +//! 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!(unsafe { abs(-2) }, 2); +/// ``` +#[no_mangle] +pub unsafe extern "C" fn abs(i: CInt) -> CInt { + i.abs() +} + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn neg() { + assert_eq!( + unsafe { abs(-2) }, + 2 + ); + } + + #[test] + fn pos() { + assert_eq!( + unsafe { abs(3) }, + 3 + ); + } + + #[test] + fn zero() { + assert_eq!( + unsafe { abs(0) }, + 0 + ); + } +} diff --git a/src/lib.rs b/src/lib.rs index 9cda140..3f0346e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; From f59465b8a8146693496dc9a88a6ba24f4c1bc5be Mon Sep 17 00:00:00 2001 From: sander Date: Fri, 19 Aug 2022 15:04:35 +0200 Subject: [PATCH 2/3] abs: rustfmt --- src/abs.rs | 37 ++++++++++++++----------------------- 1 file changed, 14 insertions(+), 23 deletions(-) diff --git a/src/abs.rs b/src/abs.rs index b85671b..5955872 100644 --- a/src/abs.rs +++ b/src/abs.rs @@ -12,34 +12,25 @@ use crate::CInt; /// ``` #[no_mangle] pub unsafe extern "C" fn abs(i: CInt) -> CInt { - i.abs() + i.abs() } #[cfg(test)] mod test { - use super::*; + use super::*; - #[test] - fn neg() { - assert_eq!( - unsafe { abs(-2) }, - 2 - ); - } + #[test] + fn neg() { + assert_eq!(unsafe { abs(-2) }, 2); + } - #[test] - fn pos() { - assert_eq!( - unsafe { abs(3) }, - 3 - ); - } + #[test] + fn pos() { + assert_eq!(unsafe { abs(3) }, 3); + } - #[test] - fn zero() { - assert_eq!( - unsafe { abs(0) }, - 0 - ); - } + #[test] + fn zero() { + assert_eq!(unsafe { abs(0) }, 0); + } } From 3166beaee091c61fb050127541d98ff9be3db7dc Mon Sep 17 00:00:00 2001 From: sander Date: Fri, 19 Aug 2022 15:32:28 +0200 Subject: [PATCH 3/3] abs: remove unsafe --- src/abs.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/abs.rs b/src/abs.rs index 5955872..c6d257b 100644 --- a/src/abs.rs +++ b/src/abs.rs @@ -8,10 +8,10 @@ use crate::CInt; /// /// ``` /// use tinyrlibc::abs; -/// assert_eq!(unsafe { abs(-2) }, 2); +/// assert_eq!(abs(-2), 2); /// ``` #[no_mangle] -pub unsafe extern "C" fn abs(i: CInt) -> CInt { +pub extern "C" fn abs(i: CInt) -> CInt { i.abs() } @@ -21,16 +21,16 @@ mod test { #[test] fn neg() { - assert_eq!(unsafe { abs(-2) }, 2); + assert_eq!(abs(-2), 2); } #[test] fn pos() { - assert_eq!(unsafe { abs(3) }, 3); + assert_eq!(abs(3), 3); } #[test] fn zero() { - assert_eq!(unsafe { abs(0) }, 0); + assert_eq!(abs(0), 0); } }