From b08b0a440226540a5157bb7ec1e82644ef228004 Mon Sep 17 00:00:00 2001 From: Casper Date: Thu, 29 Oct 2020 12:57:29 -0700 Subject: Implement `Debug` trait for Rust flatbuffers. (#6207) * Refactor idl_gen_rust to a ForAllX continuation pattern. * Removed unneeded SetValue and updated sample rust gencode * Make Rust flatbuffers print right * Generated code and removed unnecessary trait constraint * bumped rust version. Release required * removed an unwrap in Rust Debug-print unions * Tested formatting flatbuffers in rust. * Set float precision in flaky debug-print test * impl Debug for structs too Co-authored-by: Casper Neo --- rust/flatbuffers/src/vector.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'rust') diff --git a/rust/flatbuffers/src/vector.rs b/rust/flatbuffers/src/vector.rs index 3079649d..5236ea16 100644 --- a/rust/flatbuffers/src/vector.rs +++ b/rust/flatbuffers/src/vector.rs @@ -19,6 +19,7 @@ use std::marker::PhantomData; use std::mem::size_of; use std::slice::from_raw_parts; use std::str::from_utf8_unchecked; +use std::fmt::{Debug, Result, Formatter}; use crate::endian_scalar::read_scalar_at; #[cfg(target_endian = "little")] @@ -26,9 +27,19 @@ use crate::endian_scalar::EndianScalar; use crate::follow::Follow; use crate::primitives::*; -#[derive(Debug)] pub struct Vector<'a, T: 'a>(&'a [u8], usize, PhantomData); +impl<'a, T> Debug for Vector<'a, T> +where + T: 'a + Follow<'a>, + >::Inner : Debug +{ + fn fmt(&self, f: &mut Formatter) -> Result { + f.debug_list().entries(self.iter()).finish() + } +} + + // We cannot use derive for these two impls, as it would only implement Copy // and Clone for `T: Copy` and `T: Clone` respectively. However `Vector<'a, T>` // can always be copied, no matter that `T` you have. -- cgit v1.2.3