Make flatc generate Rust files not requiring std (#7273)

* Set an explicit 2018 edition for Rust tests

* Replace all `std` usage with `core` and `alloc` in Rust code generator

* Update the generated files

* Make Rust tests actually use no_std when the corresponding feature is enabled
This commit is contained in:
Bogdan Opanchuk
2022-04-26 18:40:03 -07:00
committed by GitHub
parent 9917a168cd
commit 750dde7669
88 changed files with 799 additions and 430 deletions

View File

@@ -115,7 +115,6 @@ quickcheck! {
let mut expected = x.to_le_bytes().to_vec();
expected.push(3 << 2 | 2); // Float W32.
expected.push(4); // Root width W32.
println!("{:?}: {:?} vs {:?} cmp {:?}", x, &fxb, &expected, fxb==expected);
fxb == expected
}
}
@@ -321,7 +320,7 @@ fn indirect_numbers() {
v.push(IndirectUInt(u64::max_value()));
v.push(IndirectInt(i64::min_value()));
// TODO(cneo): Something about Float EPSILON and casting leads to a different binary format.
v.push(IndirectFloat(std::f64::consts::PI));
v.push(IndirectFloat(core::f64::consts::PI));
v.push(0u32); // This is stored in 8 bits instead of 64 because of indirection.
v.end_vector();
assert_eq!(
@@ -385,7 +384,6 @@ fn indirect_2p5x_smaller() {
v.push(IndirectInt(i64::max_value()));
v.end_vector();
let len_with_indirect = builder.view().len() as f32;
dbg!(len_with_indirect, len_without_indirect);
assert!(len_with_indirect * 2.5 < len_without_indirect);
}
#[test]

View File

@@ -13,6 +13,7 @@
// limitations under the License.
mod binary_format;
#[cfg(not(feature = "no_std"))] // uses file I/O
mod interop;
mod other_api;
#[cfg(not(miri))] // slow.

View File

@@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
use alloc::vec::Vec;
use flexbuffers::*;
#[cfg(not(miri))] // slow.
use quickcheck::QuickCheck;

View File

@@ -1,7 +1,10 @@
use alloc::collections::BTreeMap;
use alloc::string::String;
use alloc::vec::Vec;
use super::rwyw::NonNullString;
use flexbuffers::*;
use quickcheck::{Arbitrary, Gen};
use std::collections::BTreeMap;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
enum Enum {

View File

@@ -12,6 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.
use alloc::string::{String, ToString};
use alloc::vec::Vec;
// Read what you wrote.
use flexbuffers::*;
#[cfg(not(miri))] // slow.
@@ -23,7 +26,7 @@ use serde::{Deserialize, Serialize};
pub struct NonNullString(String);
impl quickcheck::Arbitrary for NonNullString {
fn arbitrary<G: quickcheck::Gen>(g: &mut G) -> Self {
let size = std::cmp::min(1, usize::arbitrary(g));
let size = core::cmp::min(1, usize::arbitrary(g));
NonNullString(
(0..)
.map(|_| <char>::arbitrary(g))
@@ -74,7 +77,7 @@ quickcheck! {
}
v.end_vector();
let r = Reader::get_root(builder.view()).unwrap().as_vector();
xs.iter().enumerate().all(|(i, &x)| (r.idx(i).as_f64() - x).abs() < std::f64::EPSILON)
xs.iter().enumerate().all(|(i, &x)| (r.idx(i).as_f64() - x).abs() < core::f64::EPSILON)
}
fn qc_vec_string(xs: Vec<String>) -> bool {
let mut builder = Builder::default();
@@ -86,6 +89,7 @@ quickcheck! {
let r = Reader::get_root(builder.view()).unwrap().as_vector();
xs.iter().enumerate().all(|(i, x)| (r.idx(i).as_str() == x))
}
#[cfg(not(feature = "no_std"))]
fn qc_map_int(xs: std::collections::BTreeMap<NonNullString, i64>) -> bool {
let mut builder = Builder::default();
let mut m = builder.start_map();
@@ -98,6 +102,7 @@ quickcheck! {
r.idx(i).as_i64() == v && r.idx(k.0.as_str()).as_i64() == v
})
}
#[cfg(not(feature = "no_std"))]
fn qc_map_string(xs: std::collections::BTreeMap<NonNullString, String>) -> bool {
let mut builder = Builder::default();
let mut m = builder.start_map();
@@ -195,7 +200,6 @@ fn empty_vectors() {
let foo1 = Foo::default();
let mut s = FlexbufferSerializer::new();
foo1.serialize(&mut s).unwrap();
dbg!(s.view());
let r = Reader::get_root(s.view()).unwrap();
let foo2 = Foo::deserialize(r).unwrap();
assert_eq!(foo1, foo2);