@@ -6,6 +6,10 @@ use crate::error::Result;
66pub type DeserializeFn < T > = fn ( MessageReader ) -> Result < T > ;
77pub type SerializeFn < T > = fn ( & T , & mut Vec < u8 > ) -> Result < ( ) > ;
88
9+ /// According to https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md, grpc uses
10+ /// a four bytes to describe the length of a message, so it should not exceed u32::MAX.
11+ const MAX_MESSAGE_SIZE : usize = u32:: MAX as usize ;
12+
913/// Defines how to serialize and deserialize between the specialized type and byte slice.
1014pub struct Marshaller < T > {
1115 // Use function pointer here to simplify the signature.
@@ -28,17 +32,17 @@ pub struct Marshaller<T> {
2832pub mod pb_codec {
2933 use protobuf:: { CodedInputStream , Message } ;
3034
31- use super :: MessageReader ;
35+ use super :: { MessageReader , MAX_MESSAGE_SIZE } ;
3236 use crate :: error:: { Error , Result } ;
3337
3438 #[ inline]
3539 pub fn ser < T : Message > ( t : & T , buf : & mut Vec < u8 > ) -> Result < ( ) > {
3640 t. write_to_vec ( buf) ?;
37- if buf. len ( ) <= u32 :: MAX as usize {
41+ if buf. len ( ) <= MAX_MESSAGE_SIZE as usize {
3842 Ok ( ( ) )
3943 } else {
4044 Err ( Error :: Codec (
41- format ! ( "message is too large: {} > u32::MAX " , buf. len( ) ) . into ( ) ,
45+ format ! ( "message is too large: {} > {} " , buf. len( ) , MAX_MESSAGE_SIZE ) . into ( ) ,
4246 ) )
4347 }
4448 }
@@ -56,17 +60,17 @@ pub mod pb_codec {
5660pub mod pr_codec {
5761 use prost:: Message ;
5862
59- use super :: MessageReader ;
63+ use super :: { MessageReader , MAX_MESSAGE_SIZE } ;
6064 use crate :: error:: { Error , Result } ;
6165
6266 #[ inline]
6367 pub fn ser < M : Message > ( msg : & M , buf : & mut Vec < u8 > ) -> Result < ( ) > {
6468 msg. encode ( buf) ?;
65- if buf. len ( ) <= u32 :: MAX as usize {
69+ if buf. len ( ) <= MAX_MESSAGE_SIZE as usize {
6670 Ok ( ( ) )
6771 } else {
6872 Err ( Error :: Codec (
69- format ! ( "message is too large: {} > u32::MAX " , buf. len( ) ) . into ( ) ,
73+ format ! ( "message is too large: {} > {} " , buf. len( ) , MAX_MESSAGE_SIZE ) . into ( ) ,
7074 ) )
7175 }
7276 }
0 commit comments