Skip to content

Commit 166a842

Browse files
committed
address comment
Signed-off-by: Jay Lee <BusyJayLee@gmail.com>
1 parent bdaa6d1 commit 166a842

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

src/codec.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ use crate::error::Result;
66
pub type DeserializeFn<T> = fn(MessageReader) -> Result<T>;
77
pub 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.
1014
pub struct Marshaller<T> {
1115
// Use function pointer here to simplify the signature.
@@ -28,17 +32,17 @@ pub struct Marshaller<T> {
2832
pub 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 {
5660
pub 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

Comments
 (0)