Skip to content

Commit 59c9ea8

Browse files
committed
feat: fix bytes support
1 parent 4b8abf7 commit 59c9ea8

File tree

4 files changed

+46
-1
lines changed

4 files changed

+46
-1
lines changed

prost-build/src/code_generator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ impl<'b> CodeGenerator<'_, 'b> {
450450
let type_tag = self.field_type_tag(&field.descriptor, custom_module_path.as_deref());
451451
self.buf.push_str(&type_tag);
452452

453-
if type_ == Type::Bytes {
453+
if type_ == Type::Bytes && custom_module_path.is_none() {
454454
let bytes_type = self
455455
.context
456456
.bytes_type(fq_message_name, field.descriptor.name());

tests/build.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,11 @@ fn main() {
196196
".custom_scalar.Msg.g.key",
197197
],
198198
)
199+
.custom_scalar(
200+
prost_types::field_descriptor_proto::Type::Bytes,
201+
"crate::custom_scalar::MyVecInterface",
202+
[".custom_scalar.Msg.h"],
203+
)
199204
.compile_protos(&[src.join("custom_scalar.proto")], includes)
200205
.unwrap();
201206

tests/src/custom_scalar.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ message Msg {
1313
map<string, string> e = 5;
1414
map<string, string> f = 6;
1515
map<string, string> g = 7;
16+
bytes h = 8;
1617
}

tests/src/custom_scalar.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ fn test_custom_scalar() {
2424
.iter()
2525
.cloned()
2626
.collect(),
27+
h: MyVec(vec![1, 2]),
2728
};
2829

2930
let data = msg.encode_to_vec();
@@ -69,3 +70,41 @@ impl prost::CustomScalarInterface for MyStringInterface {
6970
}
7071
}
7172
}
73+
74+
#[derive(Clone, Default, PartialEq, Eq, Hash, Debug, PartialOrd, Ord)]
75+
pub struct MyVec(pub Vec<u8>);
76+
77+
struct MyVecInterface;
78+
79+
impl prost::CustomScalarInterface for MyVecInterface {
80+
type Type = MyVec;
81+
type RefType<'x> = &'x [u8];
82+
83+
fn encoded_len(tag: u32, value: &Self::Type) -> usize {
84+
::prost::encoding::bytes::encoded_len(tag, &value.0)
85+
}
86+
87+
fn encode(tag: u32, value: &Self::Type, buf: &mut impl prost::bytes::BufMut) {
88+
::prost::encoding::bytes::encode(tag, &value.0, buf);
89+
}
90+
91+
fn merge(
92+
wire_type: prost::encoding::WireType,
93+
value: &mut Self::Type,
94+
buf: &mut impl prost::bytes::Buf,
95+
ctx: prost::encoding::DecodeContext,
96+
) -> Result<(), prost::DecodeError> {
97+
::prost::encoding::bytes::merge(wire_type, &mut value.0, buf, ctx)
98+
}
99+
100+
fn is_default(value: &Self::Type) -> bool {
101+
value.0.is_empty()
102+
}
103+
104+
fn get<'x>(value: &'x Option<Self::Type>) -> Self::RefType<'x> {
105+
match value {
106+
Some(value) => value.0.as_slice(),
107+
None => &[],
108+
}
109+
}
110+
}

0 commit comments

Comments
 (0)