-
Notifications
You must be signed in to change notification settings - Fork 110
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix/batch length #824
Fix/batch length #824
Changes from 1 commit
4c013e8
772c46f
efb96fc
ec5cf68
a23e6eb
b35346b
6ecaa03
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ use byteorder::{BigEndian, ReadBytesExt}; | |
use bytes::{Buf, BufMut}; | ||
use num_enum::TryFromPrimitive; | ||
use std::collections::HashMap; | ||
use std::convert::TryFrom; | ||
use std::convert::{Infallible, TryFrom}; | ||
use std::convert::TryInto; | ||
use std::net::IpAddr; | ||
use std::net::SocketAddr; | ||
|
@@ -98,6 +98,12 @@ impl From<std::str::Utf8Error> for ParseError { | |
} | ||
} | ||
|
||
impl From<Infallible> for ParseError { | ||
fn from(_: Infallible) -> Self { | ||
ParseError::BadIncomingData("Unexpected Infallible Error".to_string()) | ||
} | ||
} | ||
|
||
impl From<std::array::TryFromSliceError> for ParseError { | ||
fn from(_err: std::array::TryFromSliceError) -> Self { | ||
ParseError::BadIncomingData("array try from slice failed".to_string()) | ||
|
@@ -174,10 +180,19 @@ pub fn read_short(buf: &mut &[u8]) -> Result<i16, ParseError> { | |
Ok(v) | ||
} | ||
|
||
pub fn read_u16(buf: &mut &[u8]) -> Result<u16, ParseError> { | ||
let v = buf.read_u16::<BigEndian>()?; | ||
Ok(v) | ||
} | ||
|
||
pub fn write_short(v: i16, buf: &mut impl BufMut) { | ||
buf.put_i16(v); | ||
} | ||
|
||
pub fn write_u16(v: u16, buf: &mut impl BufMut) { | ||
buf.put_u16(v); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we should change There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree about changing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The switch wasn't so simple, i had to change the type in some other structs that were linked in a way I don't understand yet. example. But as long as the tests pass, it should be fine. |
||
|
||
pub(crate) fn read_short_length(buf: &mut &[u8]) -> Result<usize, ParseError> { | ||
let v = read_short(buf)?; | ||
let v: usize = v.try_into()?; | ||
|
@@ -200,6 +215,15 @@ fn type_short() { | |
} | ||
} | ||
|
||
#[test] | ||
fn type_u16() { | ||
let vals = [0, 1, u16::MAX]; | ||
for val in vals.iter() { | ||
let mut buf = Vec::new(); | ||
write_u16(*val, &mut buf); | ||
assert_eq!(read_u16(&mut &buf[..]).unwrap(), *val); | ||
} | ||
} | ||
// https://github.com/apache/cassandra/blob/trunk/doc/native_protocol_v4.spec#L208 | ||
pub fn read_bytes_opt<'a>(buf: &mut &'a [u8]) -> Result<Option<&'a [u8]>, ParseError> { | ||
let len = read_int(buf)?; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you explain what this trait implementation does?
AFAIU
Infallible
is for things that can never happen, so why do we want to convert it to aParseError
?https://doc.rust-lang.org/std/convert/enum.Infallible.html
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's for the conversion of
u16
to ausize
. I wanted to do a simpleas
but didn't want to modify the existing code as much. I think converting from the previousi16
to ausize
would have failed with theTryFromIntError
error, but withu16
->usize
, it really is in fact infallibleThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah okay, I understand now.
So we have a few pieces of code like this one:
And after changing the
i16
tou16
they no longer compile becausetry_into()
has anInfallible
error type.We can implement a conversion from
Infallible
toParseError
to make it compile, but it's a bit hacky.I think it would be better to replace the
try_into()?
withinto()
, like this:There is a an implentation of
From<u16> for usize
, so we can just useinto()
here.