Skip to content

Commit

Permalink
Support #![no_std] builds through default std feature
Browse files Browse the repository at this point in the history
  • Loading branch information
ctrlcctrlv committed Jan 11, 2022
1 parent 13c42f9 commit e10903e
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 73 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ serde = { version = "1.0", features = ["derive"], optional = true }
num-traits = { version = "0.2", optional = true }

[features]
default = ["serde"]
default = ["serde", "std"]
hash = ["num-traits"]
std = []
16 changes: 0 additions & 16 deletions src/from_str.rs

This file was deleted.

62 changes: 6 additions & 56 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
//! This is a Rust implementation of the UFO data type "integer or float".
#![cfg_attr(not(std), no_std)]

#[cfg(feature = "default")]
use serde::{Serialize, Deserialize};

#[cfg(feature = "hash")]
mod hash;

pub mod encode;
mod from_str;
mod str_conv;
pub use str_conv::ConversionError;

/// The UFO data type "integer or float".
#[cfg_attr(feature = "default", derive(Serialize, Deserialize))]
Expand Down Expand Up @@ -191,7 +194,7 @@ impl_from_iof_for_primitives_all!(f32, f64);
impl_from_integer_for_iof_all!(i8, i16, i32, i64, isize, u8, u16, u32, u64, usize);
impl_from_float_for_iof_all!(f32, f64);

use std::ops::{Mul, Div, Add, Sub, Rem};
use core::ops::{Mul, Div, Add, Sub, Rem};
// IntegerOrFloat * IntegerOrFloat, etc.
impl_std_ops_iof_iof!(*, Mul, mul);
impl_std_ops_iof_iof!(/, Div, div);
Expand All @@ -213,63 +216,10 @@ impl_std_ops_primitives_iof_all!(+, Add, add);
impl_std_ops_primitives_iof_all!(-, Sub, sub);
impl_std_ops_primitives_iof_all!(%, Rem, rem);

use std::ops::Neg;
use core::ops::Neg;
impl Neg for IntegerOrFloat {
type Output = Self;
fn neg(self) -> Self::Output {
self * -1
}
}

impl From<IntegerOrFloat> for String {
fn from(iof: IntegerOrFloat) -> Self {
match iof {
IntegerOrFloat::Float(f) => f.to_string(),
IntegerOrFloat::Integer(i) => i.to_string()
}
}
}

impl ToString for IntegerOrFloat {
fn to_string(&self) -> String {
String::from(*self)
}
}

use std::error::Error;
use std::fmt;
#[derive(Debug, PartialEq)]
pub enum ConversionError {
IntegerConversionError,
FloatConversionError
}
use ConversionError::*;
impl fmt::Display for ConversionError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
IntegerConversionError => write!(f, "String not an integer"),
FloatConversionError => write!(f, "String not a floating point number")
}
}
}
impl Error for ConversionError {}

use std::convert::TryFrom;
impl TryFrom<&str> for IntegerOrFloat {
type Error = ConversionError;
fn try_from(s: &str) -> Result<Self, Self::Error> {
if !s.contains('.') {
if let Ok(i) = s.parse::<i32>() {
Ok(IntegerOrFloat::Integer(i))
} else {
Err(IntegerConversionError)
}
} else {
if let Ok(f) = s.parse::<f32>() {
Ok(IntegerOrFloat::Float(f))
} else {
Err(FloatConversionError)
}
}
}
}
72 changes: 72 additions & 0 deletions src/str_conv.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
use core::str::FromStr;

use super::IntegerOrFloat::{self, *};

#[cfg(std)]
impl From<IntegerOrFloat> for String {
fn from(iof: IntegerOrFloat) -> Self {
match iof {
IntegerOrFloat::Float(f) => f.to_string(),
IntegerOrFloat::Integer(i) => i.to_string()
}
}
}

#[cfg(std)]
impl ToString for IntegerOrFloat {
fn to_string(&self) -> String {
String::from(*self)
}
}

#[cfg(std)]
use std::error::Error;
#[cfg(not(std))]
trait Error {}

use core::fmt;

#[derive(Debug, PartialEq)]
pub enum ConversionError {
IntegerConversionError,
FloatConversionError
}

use ConversionError::*;
impl fmt::Display for ConversionError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
IntegerConversionError => write!(f, "String not an integer"),
FloatConversionError => write!(f, "String not a floating point number")
}
}
}

impl Error for ConversionError {}

use core::convert::TryFrom;
impl TryFrom<&str> for IntegerOrFloat {
type Error = ConversionError;
fn try_from(s: &str) -> Result<Self, Self::Error> {
if let Ok(i) = s.parse::<i32>() {
Ok(Integer(i))
} else {
if let Ok(f) = s.parse::<f32>() {
Ok(Float(f))
} else {
if !s.contains('.') {
Err(IntegerConversionError)
} else {
Err(FloatConversionError)
}
}
}
}
}

impl FromStr for IntegerOrFloat {
type Err = ConversionError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Self::try_from(s)
}
}

0 comments on commit e10903e

Please sign in to comment.