Skip to content

Commit 0f6e723

Browse files
committed
pread: remove Copy bound when generics are present, not needed; move test with generics and ctx dependent on prior fields to complex.rs file; add test showing decoding works
1 parent fabe4ea commit 0f6e723

3 files changed

Lines changed: 55 additions & 36 deletions

File tree

scroll_derive/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ fn impl_struct(
168168
syn::GenericParam::Type(t) => Some({
169169
let ident = &t.ident;
170170
quote! {
171-
#ident : ::scroll::ctx::TryFromCtx<#lifetime, ::scroll::Endian, Error = ::scroll::Error> + ::std::marker::Copy,
171+
#ident : ::scroll::ctx::TryFromCtx<#lifetime, ::scroll::Endian, Error = ::scroll::Error>,
172172
::scroll::Error : ::std::convert::From<< #ident as ::scroll::ctx::TryFromCtx<#lifetime, ::scroll::Endian>>::Error>,
173173
< #ident as ::scroll::ctx::TryFromCtx<#lifetime, ::scroll::Endian>>::Error : ::std::convert::From<scroll::Error>
174174
}

scroll_derive/tests/complex.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
use scroll::Pread;
2+
use scroll_derive::{Pread, Pwrite};
3+
4+
#[derive(Debug, Pread, Pwrite)]
5+
pub struct FloofHeader {
6+
pub boop: u8,
7+
pub length: u16,
8+
pub flag: u8,
9+
pub quux_service: u8,
10+
pub quux_client_id: u8,
11+
}
12+
13+
pub trait QuuxHeaderApi {
14+
fn msg_len(&self) -> usize;
15+
}
16+
17+
#[derive(Debug, Pread, Pwrite)]
18+
pub struct QuuxHeader {
19+
pub typ: u8,
20+
pub txn_id: u16,
21+
pub msg_id: u16,
22+
pub msg_len: u16,
23+
}
24+
25+
impl QuuxHeaderApi for QuuxHeader {
26+
fn msg_len(&self) -> usize {
27+
self.msg_len as usize
28+
}
29+
}
30+
31+
#[derive(Debug, Pread, Pwrite)]
32+
pub struct Response<'a, T: QuuxHeaderApi> {
33+
pub floof: FloofHeader,
34+
pub quux: T,
35+
#[scroll(ctx = quux.msg_len())]
36+
pub tlvs: &'a [u8],
37+
}
38+
39+
#[test]
40+
fn test_pread_generics_with_ctx_dependent_on_prior_field() {
41+
let bytes = [
42+
1, 70, 0, 128, 9, 3, 4, 1, 0, 46, 0, 58, 0, 1, 8, 0, 1, 1, 4, 0, 1, 6, 0, 0, 16, 16, 0, 1,
43+
1, 0, 12, 43, 47, 52, 48, 49, 57, 42, 54, 49, 53, 50, 51, 39, 3, 0, 1, 1, 2, 43, 19, 0, 1,
44+
1, 0, 0, 0, 1, 12, 41, 49, 51, 48, 49, 57, 42, 54, 43, 51, 50, 54,
45+
];
46+
let resp: Response<QuuxHeader> = bytes.pread(0).unwrap();
47+
assert_eq!(resp.floof.flag, 0x80);
48+
assert_eq!(resp.floof.length as usize, bytes.len() - 1);
49+
assert_eq!(resp.floof.quux_service, 0x9);
50+
assert_eq!(resp.floof.quux_client_id, 3);
51+
assert_eq!(resp.quux.msg_len(), resp.tlvs.len());
52+
assert_eq!(resp.quux.typ, 0x4);
53+
assert_eq!(resp.quux.txn_id, 1);
54+
}

scroll_derive/tests/tests.rs

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -302,38 +302,3 @@ fn test_pread_lifetime() {
302302
b2.pwrite(&data, 0).unwrap();
303303
assert_eq!(b2, bytes);
304304
}
305-
306-
#[derive(Debug, Pread, Pwrite)]
307-
pub struct FloofHeader {
308-
pub boop: u8,
309-
pub length: u16,
310-
pub flag: u8,
311-
pub quux_service: u8,
312-
pub quux_client_id: u8,
313-
}
314-
315-
pub trait QuuxHeaderApi {
316-
fn msg_len(&self) -> usize;
317-
}
318-
319-
#[derive(Debug, Copy, Clone, Pread, Pwrite)]
320-
pub struct QuuxHeader {
321-
pub typ: u8,
322-
pub txn_id: u16,
323-
pub msg_id: u16,
324-
pub msg_len: u16,
325-
}
326-
327-
impl QuuxHeaderApi for QuuxHeader {
328-
fn msg_len(&self) -> usize {
329-
self.msg_len as usize
330-
}
331-
}
332-
333-
#[derive(Debug, Pread, Pwrite)]
334-
pub struct Response<'a, T: QuuxHeaderApi> {
335-
pub floof: FloofHeader,
336-
pub quux: T,
337-
#[scroll(ctx = quux.msg_len())]
338-
pub tlvs: &'a [u8],
339-
}

0 commit comments

Comments
 (0)