Skip to content

Commit 71d27b6

Browse files
Support no_std targets
This adds `no_std` support by - adding a default `std` feature for disabling `impl Error` on `no_std`. - adding the Hashbrown dependency for `no_std` hash sets Hashbrown also provides the `HashSet` implementation in `std`. `std`'s `HashSet` is not in liballoc yet, though. By default, Hashbrown uses AHash as the default hasher. AHash is much faster than `std`'s SipHash, but is not as HashDoS resistant. That seemed not relevant to me. Co-authored-by: Andreea Florescu <[email protected]> Signed-off-by: Martin Kröning <[email protected]>
1 parent d5fe563 commit 71d27b6

File tree

5 files changed

+42
-8
lines changed

5 files changed

+42
-8
lines changed

.buildkite/custom-tests.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"tests": [
3+
{
4+
"test_name": "build-alloc",
5+
"command": "rustup target add {target_platform}-unknown-none && env RUSTFLAGS=\"-D warnings\" cargo build --release --target {target_platform}-unknown-none --no-default-features --features alloc",
6+
"platform": [
7+
"x86_64",
8+
"aarch64"
9+
]
10+
}
11+
]
12+
}

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Upcoming Release
22

3+
## Added
4+
- [[#68](https://github.com/rust-vmm/vm-fdt/pull/68)] Implemented `no_std`
5+
support by adding a default `std` feature.
6+
37
## Fixed
48
- [[#69](https://github.com/rust-vmm/vm-fdt/pull/69)] Fixed
59
`clippy::incorrect_partial_ord_impl_on_ord_type`.

Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@ edition = "2018"
88
repository = "https://github.com/rust-vmm/vm-fdt"
99
keywords = ["fdt"]
1010

11+
[dependencies]
12+
hashbrown = { version = "0.14", optional = true }
13+
1114
[features]
15+
default = ["std"]
16+
std = []
17+
alloc = ["hashbrown"]
1218
# This feature was added as a way to exclude long running tests in development.
1319
# `cargo test` does not run tests for all features by default. The vm-fdt
1420
# developer can thus choose to "hide" a long running under this feature.

src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright 2021 The Chromium OS Authors. All rights reserved.
22
// SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause
33

4+
#![cfg_attr(not(feature = "std"), no_std)]
45
#![deny(missing_docs)]
56

67
//! This crate provides the ability to manipulate Flattened Devicetree blobs.
@@ -81,6 +82,9 @@
8182
//! # let dtb = create_fdt().unwrap();
8283
//! ```
8384
85+
#[macro_use]
86+
extern crate alloc;
87+
8488
mod writer;
8589

8690
pub use writer::Result as FdtWriterResult;

src/writer.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,19 @@
44
//! This module writes Flattened Devicetree blobs as defined here:
55
//! <https://devicetree-specification.readthedocs.io/en/stable/flattened-format.html>
66
7-
use std::cmp::{Ord, Ordering};
8-
use std::collections::{BTreeMap, HashSet};
9-
use std::convert::TryInto;
10-
use std::ffi::CString;
11-
use std::fmt;
12-
use std::mem::size_of_val;
7+
use alloc::collections::BTreeMap;
8+
use alloc::ffi::CString;
9+
use alloc::string::String;
10+
use alloc::vec::Vec;
11+
use core::cmp::{Ord, Ordering};
12+
use core::convert::TryInto;
13+
use core::fmt;
14+
use core::mem::size_of_val;
15+
#[cfg(feature = "std")]
16+
use std::collections::HashSet;
17+
18+
#[cfg(all(feature = "alloc", not(feature = "std")))]
19+
use hashbrown::HashSet;
1320

1421
use crate::{
1522
FDT_BEGIN_NODE, FDT_END, FDT_END_NODE, FDT_MAGIC, FDT_PROP, NODE_NAME_MAX_LEN,
@@ -75,10 +82,11 @@ impl fmt::Display for Error {
7582
}
7683
}
7784

85+
#[cfg(feature = "std")]
7886
impl std::error::Error for Error {}
7987

8088
/// Result of a FDT writer operation.
81-
pub type Result<T> = std::result::Result<T, Error>;
89+
pub type Result<T> = core::result::Result<T, Error>;
8290

8391
const FDT_HEADER_SIZE: usize = 40;
8492
const FDT_VERSION: u32 = 17;
@@ -308,7 +316,7 @@ impl FdtWriter {
308316

309317
// Append `num_bytes` padding bytes (0x00).
310318
fn pad(&mut self, num_bytes: usize) {
311-
self.data.extend(std::iter::repeat(0).take(num_bytes));
319+
self.data.extend(core::iter::repeat(0).take(num_bytes));
312320
}
313321

314322
// Append padding bytes (0x00) until the length of data is a multiple of `alignment`.

0 commit comments

Comments
 (0)