Skip to content

Commit

Permalink
Fix schema implementation for slice references
Browse files Browse the repository at this point in the history
Also fix the schema test that was never running.
  • Loading branch information
ia0 committed May 13, 2024
1 parent 14237bc commit 22c21e1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ impl<T: Schema, E: Schema> Schema for Result<T, E> {
};
}

impl<T: Schema> Schema for &'_ T {
impl<T: Schema + ?Sized> Schema for &'_ T {
const SCHEMA: &'static NamedType = T::SCHEMA;
}

Expand Down
31 changes: 27 additions & 4 deletions tests/schema.rs
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -97,11 +103,28 @@ 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]),
}
}
]),
},
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
);
}

0 comments on commit 22c21e1

Please sign in to comment.