Skip to content

Commit d4e0778

Browse files
authored
Merge pull request #46 from hushmesh/no_std
add no_std support
2 parents 1ed258d + 1725bf7 commit d4e0778

8 files changed

Lines changed: 50 additions & 18 deletions

File tree

.github/workflows/ci.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,18 @@ jobs:
1515
- run: rustup update ${{ matrix.toolchain }}
1616
- run: cargo check --all-features --all
1717

18+
nostd:
19+
name: Check no_std
20+
runs-on: ubuntu-latest
21+
strategy:
22+
matrix:
23+
toolchain: [stable, beta, nightly]
24+
steps:
25+
- name: Checkout sources
26+
uses: actions/checkout@v4
27+
- run: rustup update ${{ matrix.toolchain }}
28+
- run: cargo check --no-default-features --features=serde --all
29+
1830
test:
1931
name: Test Suite
2032
runs-on: ubuntu-latest

Cargo.toml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,26 @@ description = "Memory-efficient data structures based on patricia tree"
66
homepage = "https://github.com/sile/patricia_tree"
77
repository = "https://github.com/sile/patricia_tree"
88
readme = "README.md"
9-
keywords = ["patricia", "radix", "trie"]
9+
keywords = ["patricia", "radix", "trie", "no_std"]
1010
categories = ["data-structures"]
1111
license = "MIT"
1212
edition = "2021"
1313
rust-version = "1.82.0"
1414

1515
[dependencies]
16-
bitflags = "2"
16+
bitflags = { version = "2", default-features = false }
1717
serde = { version = "1", optional = true }
1818

1919
[dev-dependencies]
2020
clap = { version = "4", features = ["derive"] }
2121
rand = "0.8"
2222
serde_json = { version = "1" }
2323

24+
[features]
25+
default = ["std"]
26+
std = []
27+
serde = ["dep:serde"]
28+
2429
[package.metadata.docs.rs]
2530
all-features = true
2631

src/lib.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,20 @@
2424
//! ```
2525
#![warn(missing_docs)]
2626
#![allow(clippy::cast_ptr_alignment)]
27+
#![cfg_attr(not(feature = "std"), no_std)]
2728

2829
#[macro_use]
2930
extern crate bitflags;
3031
#[cfg(test)]
3132
extern crate rand;
3233

33-
use std::cmp::Ordering;
34+
#[macro_use]
35+
extern crate alloc;
36+
37+
use alloc::borrow::ToOwned;
38+
use alloc::string::String;
39+
use alloc::vec::Vec;
40+
use core::cmp::Ordering;
3441

3542
pub use map::{GenericPatriciaMap, PatriciaMap, StringPatriciaMap};
3643
pub use set::{GenericPatriciaSet, PatriciaSet, StringPatriciaSet};
@@ -129,11 +136,11 @@ impl BorrowedBytes for str {
129136
}
130137

131138
fn is_valid_bytes(bytes: &[u8]) -> bool {
132-
std::str::from_utf8(bytes).is_ok()
139+
core::str::from_utf8(bytes).is_ok()
133140
}
134141

135142
fn from_bytes(bytes: &[u8]) -> &Self {
136-
std::str::from_utf8(bytes).expect("unreachable")
143+
core::str::from_utf8(bytes).expect("unreachable")
137144
}
138145

139146
fn strip_common_prefix(&self, bytes: &[u8]) -> &Self {

src/map.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@ use crate::node;
44
use crate::node::Node;
55
use crate::tree::{self, PatriciaTree};
66
use crate::{BorrowedBytes, Bytes};
7-
use std::fmt;
8-
use std::iter::FromIterator;
9-
use std::marker::PhantomData;
7+
use alloc::borrow::ToOwned;
8+
use alloc::string::String;
9+
use alloc::vec::Vec;
10+
use core::fmt;
11+
use core::iter::FromIterator;
12+
use core::marker::PhantomData;
1013

1114
/// Patricia tree based map with [`Vec<u8>`] as key.
1215
pub type PatriciaMap<V> = GenericPatriciaMap<Vec<u8>, V>;

src/node.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
//! A node which represents a subtree of a patricia tree.
22
use crate::BorrowedBytes;
3-
use std::alloc::{alloc, dealloc, handle_alloc_error, Layout};
4-
use std::marker::PhantomData;
5-
use std::mem;
6-
use std::ptr;
7-
use std::slice;
3+
use alloc::alloc::{alloc, dealloc, handle_alloc_error, Layout};
4+
use alloc::vec::Vec;
5+
use core::marker::PhantomData;
6+
use core::{mem, ptr, slice};
87

98
macro_rules! assert_some {
109
($expr:expr) => {

src/serialization.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
use crate::node::{Flags, Node};
22
use crate::{BorrowedBytes, GenericPatriciaMap, GenericPatriciaSet};
3+
use alloc::borrow::{Cow, ToOwned};
4+
use alloc::vec::Vec;
5+
use core::borrow::Borrow;
6+
use core::marker::PhantomData;
37
use serde::de::{Error, Visitor};
48
use serde::{Deserialize, Deserializer, Serialize, Serializer};
5-
use std::borrow::{Borrow, Cow};
6-
use std::marker::PhantomData;
79

810
impl<T> Serialize for GenericPatriciaSet<T> {
911
/// In order to serialize a [PatriciaSet], make sure you installed the crate
@@ -217,7 +219,7 @@ struct BytesVisitor;
217219
impl<'de> Visitor<'de> for BytesVisitor {
218220
type Value = Bytes<'de>;
219221

220-
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
222+
fn expecting(&self, formatter: &mut core::fmt::Formatter) -> core::fmt::Result {
221223
write!(formatter, "a byte string")
222224
}
223225

src/set.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ use crate::map::{self, GenericPatriciaMap};
33
#[cfg(any(feature = "serde", test))]
44
use crate::node::Node;
55
use crate::Bytes;
6-
use std::fmt;
7-
use std::iter::FromIterator;
6+
use alloc::string::String;
7+
use alloc::vec::Vec;
8+
use core::fmt;
9+
use core::iter::FromIterator;
810

911
/// Patricia tree based set with [`Vec<u8>`] as key.
1012
pub type PatriciaSet = GenericPatriciaSet<Vec<u8>>;

src/tree.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use alloc::vec::Vec;
2+
13
use crate::{
24
node::{self, Node, NodeMut},
35
BorrowedBytes,

0 commit comments

Comments
 (0)