From 22c21e13042cc293d87dc712567636e74d6231a1 Mon Sep 17 00:00:00 2001 From: Julien Cretin Date: Mon, 13 May 2024 14:27:51 +0200 Subject: [PATCH] Fix schema implementation for slice references Also fix the schema test that was never running. --- src/schema.rs | 2 +- tests/schema.rs | 31 +++++++++++++++++++++++++++---- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/schema.rs b/src/schema.rs index 6c3f5cb..3e62e39 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -210,7 +210,7 @@ impl Schema for Result { }; } -impl Schema for &'_ T { +impl Schema for &'_ T { const SCHEMA: &'static NamedType = T::SCHEMA; } diff --git a/tests/schema.rs b/tests/schema.rs index 911b251..138fa00 100644 --- a/tests/schema.rs +++ b/tests/schema.rs @@ -1,6 +1,6 @@ -#![cfg(feature = "derive")] +#![cfg(feature = "experimental-derive")] -use postcard::schema::{NamedType, NamedValue, NamedVariant, Schema, SdmTy, Varint}; +use postcard::experimental::schema::{NamedType, NamedValue, NamedVariant, Schema, SdmTy, Varint}; const U8_SCHEMA: NamedType = NamedType { name: "u8", @@ -40,7 +40,13 @@ struct Outer { b: u64, c: u8, d: Inner, - e: [u8; 10], + e: [u8; 3], +} + +#[allow(unused)] +#[derive(Schema)] +struct Slice<'a> { + x: &'a [u8], } #[test] @@ -97,7 +103,7 @@ fn test_struct_serialize() { name: "e", ty: &NamedType { name: "[T; N]", - ty: &SdmTy::Seq(&U8_SCHEMA), + ty: &SdmTy::Tuple(&[&U8_SCHEMA, &U8_SCHEMA, &U8_SCHEMA]), } } ]), @@ -105,3 +111,20 @@ fn test_struct_serialize() { Outer::SCHEMA ); } + +#[test] +fn test_slice_serialize() { + assert_eq!( + &NamedType { + name: "Slice", + ty: &SdmTy::Struct(&[&NamedValue { + name: "x", + ty: &NamedType { + name: "&[T]", + ty: &SdmTy::Seq(&U8_SCHEMA) + } + },]), + }, + Slice::SCHEMA + ); +}