Skip to content

Commit 53c4d1d

Browse files
committed
Support alloc-only no_std platforms
1 parent b04736f commit 53c4d1d

File tree

9 files changed

+32
-17
lines changed

9 files changed

+32
-17
lines changed

rust/flatbuffers/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,7 @@ categories = ["encoding", "data-structures", "memory-management"]
1111

1212
[dependencies]
1313
smallvec = "0.6"
14+
15+
[features]
16+
default = ["std"]
17+
std = []

rust/flatbuffers/src/builder.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@
1616

1717
extern crate smallvec;
1818

19-
use std::cmp::max;
20-
use std::marker::PhantomData;
21-
use std::ptr::write_bytes;
22-
use std::slice::from_raw_parts;
19+
#[cfg(not(feature = "std"))]
20+
use alloc::{vec, vec::Vec};
21+
22+
use core::cmp::max;
23+
use core::marker::PhantomData;
24+
use core::ptr::write_bytes;
25+
use core::slice::from_raw_parts;
2326

2427
use endian_scalar::{read_scalar, emplace_scalar};
2528
use primitives::*;
@@ -605,7 +608,7 @@ impl<'fbb> FlatBufferBuilder<'fbb> {
605608
fn assert_nested(&self, fn_name: &'static str) {
606609
// we don't assert that self.field_locs.len() >0 because the vtable
607610
// could be empty (e.g. for empty tables, or for all-default values).
608-
debug_assert!(self.nested, format!("incorrect FlatBufferBuilder usage: {} must be called while in a nested state", fn_name));
611+
debug_assert!(self.nested, "incorrect FlatBufferBuilder usage: {} must be called while in a nested state", fn_name);
609612
}
610613
#[inline]
611614
fn assert_not_nested(&self, msg: &'static str) {

rust/flatbuffers/src/endian_scalar.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
use std::mem::size_of;
17+
use core::mem::size_of;
1818

1919
/// Trait for values that must be stored in little-endian byte order, but
2020
/// might be represented in memory as big-endian. Every type that implements

rust/flatbuffers/src/follow.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
use std::marker::PhantomData;
17+
use core::marker::PhantomData;
1818

1919
/// Follow is a trait that allows us to access FlatBuffers in a declarative,
2020
/// type safe, and fast way. They compile down to almost no code (after

rust/flatbuffers/src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@
2828
//! At this time, to generate Rust code, you will need the latest `master` version of `flatc`, available from here: https://github.com/google/flatbuffers
2929
//! (On OSX, you can install FlatBuffers from `HEAD` with the Homebrew package manager.)
3030
31+
#![cfg_attr(not(feature = "std"), no_std)]
32+
33+
#[cfg(feature = "std")]
34+
extern crate core;
35+
36+
#[cfg(not(feature = "std"))]
37+
extern crate alloc;
38+
3139
mod builder;
3240
mod endian_scalar;
3341
mod follow;

rust/flatbuffers/src/primitives.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
* limitations under the License.
1515
*/
1616

17-
use std::marker::PhantomData;
18-
use std::mem::size_of;
19-
use std::ops::Deref;
17+
use core::marker::PhantomData;
18+
use core::mem::size_of;
19+
use core::ops::Deref;
2020

2121
use endian_scalar::{emplace_scalar, read_scalar, read_scalar_at};
2222
use follow::Follow;

rust/flatbuffers/src/push.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
* limitations under the License.
1515
*/
1616

17-
use std::cmp::max;
18-
use std::mem::{align_of, size_of};
17+
use core::cmp::max;
18+
use core::mem::{align_of, size_of};
1919

2020
use endian_scalar::emplace_scalar;
2121

rust/flatbuffers/src/vector.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
* limitations under the License.
1515
*/
1616

17-
use std::marker::PhantomData;
18-
use std::mem::size_of;
19-
use std::slice::from_raw_parts;
20-
use std::str::from_utf8_unchecked;
17+
use core::marker::PhantomData;
18+
use core::mem::size_of;
19+
use core::slice::from_raw_parts;
20+
use core::str::from_utf8_unchecked;
2121

2222
#[cfg(target_endian = "little")]
2323
use endian_scalar::EndianScalar;

rust/flatbuffers/src/vtable_writer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
use std::ptr::write_bytes;
17+
use core::ptr::write_bytes;
1818

1919
use endian_scalar::{emplace_scalar, read_scalar};
2020
use primitives::*;

0 commit comments

Comments
 (0)