Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make crate no_std compatible #67

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/detect.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use alloc::vec::Vec;
use hashbrown::HashMap;

use crate::constants::{MAX_TOTAL_DISTANCE, MAX_TRIGRAM_DISTANCE};
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions src/detector.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use alloc::vec::Vec;

use crate::detect;
use crate::info::Info;
use crate::lang::Lang;
Expand Down
7 changes: 4 additions & 3 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::error::Error as StdError;
use std::fmt::{self, Display};
use alloc::string::String;
use core::fmt::{self, Display};

#[derive(Debug)]
pub enum Error {
Expand All @@ -20,4 +20,5 @@ impl Display for Error {
}
}

impl StdError for Error {}
#[cfg(feature = "std")]
impl std::error::Error for Error {}
7 changes: 5 additions & 2 deletions src/lang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 alloc::string::String;
use core::fmt;
use core::str::FromStr;

use crate::error::Error;
use crate::trigrams::Trigram;
Expand Down Expand Up @@ -19045,6 +19046,8 @@ impl FromStr for Lang {
type Err = Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
#[cfg(not(feature = "std"))]
use alloc::string::ToString;
Lang::from_code(s).ok_or_else(|| Error::ParseLang(s.to_string()))
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 alloc;
extern crate core;

mod constants;
mod detect;
mod detector;
Expand Down
1 change: 1 addition & 0 deletions src/options.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::lang::Lang;
use alloc::vec::Vec;

#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum List {
Expand Down
6 changes: 4 additions & 2 deletions src/scripts/script.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -125,6 +125,8 @@ impl FromStr for Script {
type Err = Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
#[cfg(not(feature = "std"))]
use crate::alloc::string::ToString;
match s.to_lowercase().trim() {
"latin" => Ok(Script::Latin),
"cyrillic" => Ok(Script::Cyrillic),
Expand Down
1 change: 1 addition & 0 deletions src/trigrams.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use alloc::vec::Vec;
use hashbrown::HashMap;

use crate::constants::TEXT_TRIGRAMS_SIZE;
Expand Down