From e4098ba3720db827f36aedde7d071d8a1c5fe6e2 Mon Sep 17 00:00:00 2001 From: fschutt Date: Wed, 3 Feb 2021 21:06:21 +0100 Subject: [PATCH 1/2] Make crate no_std compatible --- .travis.yml | 1 + Cargo.toml | 4 ++++ src/detect.rs | 3 ++- src/detector.rs | 2 ++ src/error.rs | 7 ++++--- src/lang.rs | 7 +++++-- src/lib.rs | 6 ++++++ src/options.rs | 1 + src/scripts/script.rs | 6 ++++-- src/trigrams.rs | 1 + 10 files changed, 30 insertions(+), 8 deletions(-) diff --git a/.travis.yml b/.travis.yml index ed60f41..8ba3ab1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,6 +5,7 @@ install: - rustup component add rustfmt - rustup component add clippy script: + - cargo check --no-default-features - cargo fmt -- --check - cargo clippy -- -D warnings - cargo test diff --git a/Cargo.toml b/Cargo.toml index 8a2c3f1..0496bbc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,6 +30,10 @@ serde_json = "1.0.39" bencher = "0.1.5" proptest = "0.9.1" +[features] +default = ["std"] +std = [] + [[bench]] name = "example" harness = false diff --git a/src/detect.rs b/src/detect.rs index b24f2a7..90f4853 100644 --- a/src/detect.rs +++ b/src/detect.rs @@ -1,3 +1,4 @@ +use alloc::vec::Vec; use hashbrown::HashMap; use crate::constants::{MAX_TOTAL_DISTANCE, MAX_TRIGRAM_DISTANCE}; @@ -84,7 +85,7 @@ fn detect_lang_in_profiles( options: &Options, lang_profile_list: LangProfileList, ) -> Option<(Lang, f64)> { - let mut lang_distances: Vec<(Lang, u32)> = vec![]; + let mut lang_distances: Vec<(Lang, u32)> = Vec::new(); let trigrams = get_trigrams_with_positions(text); for &(ref lang, lang_trigrams) in lang_profile_list { diff --git a/src/detector.rs b/src/detector.rs index 6db241a..855c62d 100644 --- a/src/detector.rs +++ b/src/detector.rs @@ -1,3 +1,5 @@ +use alloc::vec::Vec; + use crate::detect; use crate::info::Info; use crate::lang::Lang; diff --git a/src/error.rs b/src/error.rs index cd7f8e6..0988684 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,5 +1,5 @@ -use std::error::Error as StdError; -use std::fmt::{self, Display}; +use core::fmt::{self, Display}; +use alloc::string::String; #[derive(Debug)] pub enum Error { @@ -20,4 +20,5 @@ impl Display for Error { } } -impl StdError for Error {} +#[cfg(feature = "std")] +impl std::error::Error for Error {} diff --git a/src/lang.rs b/src/lang.rs index dd2a5e3..9b0f9d9 100644 --- a/src/lang.rs +++ b/src/lang.rs @@ -2,8 +2,9 @@ // This file is generated automatically. // Edit misc/lang.rs.erb template instead of editing lang.rs file directly. -use std::fmt; -use std::str::FromStr; +use core::fmt; +use core::str::FromStr; +use alloc::string::String; use crate::error::Error; use crate::trigrams::Trigram; @@ -19045,6 +19046,8 @@ impl FromStr for Lang { type Err = Error; fn from_str(s: &str) -> Result { + #[cfg(not(feature = "std"))] + use alloc::string::ToString; Lang::from_code(s).ok_or_else(|| Error::ParseLang(s.to_string())) } } diff --git a/src/lib.rs b/src/lib.rs index 6241f65..e9d7998 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -31,6 +31,12 @@ //! let lang = detector.detect_lang("There is no reason not to learn Esperanto."); //! assert_eq!(lang, Some(Lang::Eng)); //! + +#![cfg_attr(not(feature = "std"), no_std)] + +extern crate core; +extern crate alloc; + mod constants; mod detect; mod detector; diff --git a/src/options.rs b/src/options.rs index 9220273..4aed9d4 100644 --- a/src/options.rs +++ b/src/options.rs @@ -1,4 +1,5 @@ use crate::lang::Lang; +use alloc::vec::Vec; #[derive(Debug, Clone, PartialEq, Eq)] pub(crate) enum List { diff --git a/src/scripts/script.rs b/src/scripts/script.rs index fc32cd8..16d0f3a 100644 --- a/src/scripts/script.rs +++ b/src/scripts/script.rs @@ -1,5 +1,5 @@ -use std::fmt; -use std::str::FromStr; +use core::fmt; +use core::str::FromStr; use super::lang_mapping; use crate::error::Error; @@ -125,6 +125,8 @@ impl FromStr for Script { type Err = Error; fn from_str(s: &str) -> Result { + #[cfg(not(feature = "std"))] + use crate::alloc::string::ToString; match s.to_lowercase().trim() { "latin" => Ok(Script::Latin), "cyrillic" => Ok(Script::Cyrillic), diff --git a/src/trigrams.rs b/src/trigrams.rs index 7a8faa7..fae9114 100644 --- a/src/trigrams.rs +++ b/src/trigrams.rs @@ -1,4 +1,5 @@ use hashbrown::HashMap; +use alloc::vec::Vec; use crate::constants::TEXT_TRIGRAMS_SIZE; use crate::utils::is_stop_char; From 09766f8d044170cfc8f3c0b3db41ec3dc5c6cb3c Mon Sep 17 00:00:00 2001 From: fschutt Date: Thu, 4 Feb 2021 10:53:32 +0100 Subject: [PATCH 2/2] Fix formatting errors (reorder imports in alphabetical order) --- src/error.rs | 2 +- src/lang.rs | 2 +- src/lib.rs | 2 +- src/trigrams.rs | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/error.rs b/src/error.rs index 0988684..8918fd1 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,5 +1,5 @@ -use core::fmt::{self, Display}; use alloc::string::String; +use core::fmt::{self, Display}; #[derive(Debug)] pub enum Error { diff --git a/src/lang.rs b/src/lang.rs index 9b0f9d9..b75f24c 100644 --- a/src/lang.rs +++ b/src/lang.rs @@ -2,9 +2,9 @@ // This file is generated automatically. // Edit misc/lang.rs.erb template instead of editing lang.rs file directly. +use alloc::string::String; use core::fmt; use core::str::FromStr; -use alloc::string::String; use crate::error::Error; use crate::trigrams::Trigram; diff --git a/src/lib.rs b/src/lib.rs index e9d7998..4f0c196 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -34,8 +34,8 @@ #![cfg_attr(not(feature = "std"), no_std)] -extern crate core; extern crate alloc; +extern crate core; mod constants; mod detect; diff --git a/src/trigrams.rs b/src/trigrams.rs index fae9114..41a4815 100644 --- a/src/trigrams.rs +++ b/src/trigrams.rs @@ -1,5 +1,5 @@ -use hashbrown::HashMap; use alloc::vec::Vec; +use hashbrown::HashMap; use crate::constants::TEXT_TRIGRAMS_SIZE; use crate::utils::is_stop_char;