Skip to content

Using Bytes on a &Vec<u8> field #460

Answered by jonasbb
theastrallyforged asked this question in Q&A
Discussion options

You must be logged in to vote

In general, the type you write inside the serde_as attribute should mirror the field type. The only exceptions are the places where you want to change the behavior, such as for Vec<u8> -> Bytes or BTreeMap<X, Y> -> Vec<(X, Y)>. This also applies to references & which should be mirrored too.

This means you can write:

#[serde_as(as = "&Bytes")]
vec: &'a Vec<u8>,

This is possible due to the generic SerializeAs implementation for &T:

impl<'a, T, U> SerializeAs<&'a T> for &'a U

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by jonasbb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #459 on May 26, 2022 10:23.