Skip to content

Commit 0b471e1

Browse files
feat: support no_std (#74)
1 parent 01f548d commit 0b471e1

File tree

5 files changed

+15
-6
lines changed

5 files changed

+15
-6
lines changed

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,6 @@ required-features = ["compat"]
6161
[[test]]
6262
name = "special_pinyin"
6363
required-features = ["with_tone_num_end", "heteronym"]
64+
65+
[dependencies]
66+
hashbrown = { version = "0.16.1", features = ["alloc"] }

src/compat.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#![allow(deprecated)]
22

33
use crate::{Pinyin, ToPinyin, ToPinyinMulti};
4-
use std::collections::HashSet;
4+
use alloc::string::{String, ToString};
5+
use alloc::vec::Vec;
56

67
/// 拼音风格
78
#[deprecated = "请使用 `Pinyin` 的方法代替"]
@@ -86,7 +87,7 @@ pub fn pinyin(s: &str, a: &Args) -> Vec<Vec<String>> {
8687
s.to_pinyin_multi()
8788
.map(|multi| match multi {
8889
Some(multi) => {
89-
let mut set = HashSet::new();
90+
let mut set = hashbrown::HashSet::new();
9091
multi
9192
.into_iter()
9293
.map(|pinyin| apply_style(pinyin, &a.style))

src/lib.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
#![no_std]
12
use crate::data::CHAR_BLOCKS;
2-
use std::convert::TryFrom;
3+
use core::convert::TryFrom;
4+
5+
#[macro_use]
6+
extern crate alloc;
37

48
#[cfg(feature = "compat")]
59
mod compat;
@@ -17,7 +21,7 @@ pub use crate::pinyin_multi::{PinyinMulti, PinyinMultiIter, PinyinMultiStrIter,
1721
/// 将给定输入字符串的拼音通过给定映射函数后存入 `Vec` 中
1822
///
1923
/// 这个函数会跳过任何没有拼音的字符。本函数主要用于测试目的。
20-
pub fn to_pinyin_vec<F>(input: &str, f: F) -> Vec<&'static str>
24+
pub fn to_pinyin_vec<F>(input: &str, f: F) -> alloc::vec::Vec<&'static str>
2125
where
2226
F: Fn(Pinyin) -> &'static str,
2327
{

src/pinyin.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::data::PINYIN_DATA;
22
use crate::{get_block_and_index, PinyinData};
3-
use std::str::Chars;
3+
use core::str::Chars;
44

55
/// 单个字符的拼音信息
66
#[derive(Copy, Clone, Debug, PartialEq, Eq)]

src/pinyin_multi.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::data::{HETERONYM_TABLE, PINYIN_DATA};
22
use crate::{get_block_and_index, Pinyin, PinyinData};
3-
use std::str::Chars;
3+
use core::str::Chars;
44

55
/// 单个字符的多音字信息
66
///
@@ -194,6 +194,7 @@ mod tests {
194194
#[test]
195195
#[cfg(feature = "with_tone")]
196196
fn str_to_pinyin_multi() {
197+
use alloc::vec::Vec;
197198
let actual = "还没"
198199
.to_pinyin_multi()
199200
.map(|multi| {

0 commit comments

Comments
 (0)