Skip to content
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

Use less strict (compatable) int types for encoding #324

Merged
merged 3 commits into from
Apr 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 19 additions & 15 deletions edgedb-protocol/src/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,32 +330,33 @@ pub fn scalar_codec(uuid: &UuidVal) -> Result<Arc<dyn Codec>, CodecError> {
}
}

impl Codec for Int32 {
impl Codec for Int16 {
fn decode(&self, buf: &[u8]) -> Result<Value, DecodeError> {
RawCodec::decode(buf).map(Value::Int32)
RawCodec::decode(buf).map(Value::Int16)
}
fn encode(&self, buf: &mut BytesMut, val: &Value) -> Result<(), EncodeError> {
let &val = match val {
Value::Int32(val) => val,
Value::Int16(val) => val,
_ => Err(errors::invalid_value(type_name::<Self>(), val))?,
};
buf.reserve(4);
buf.put_i32(val);
buf.reserve(2);
buf.put_i16(val);
Ok(())
}
}

impl Codec for Int16 {
impl Codec for Int32 {
fn decode(&self, buf: &[u8]) -> Result<Value, DecodeError> {
RawCodec::decode(buf).map(Value::Int16)
RawCodec::decode(buf).map(Value::Int32)
}
fn encode(&self, buf: &mut BytesMut, val: &Value) -> Result<(), EncodeError> {
let &val = match val {
Value::Int16(val) => val,
let val = match val {
Value::Int32(val) => *val,
Value::Int16(val) => *val as i32,
_ => Err(errors::invalid_value(type_name::<Self>(), val))?,
};
buf.reserve(2);
buf.put_i16(val);
buf.reserve(4);
buf.put_i32(val);
Ok(())
}
}
Expand All @@ -365,8 +366,10 @@ impl Codec for Int64 {
RawCodec::decode(buf).map(Value::Int64)
}
fn encode(&self, buf: &mut BytesMut, val: &Value) -> Result<(), EncodeError> {
let &val = match val {
Value::Int64(val) => val,
let val = match val {
Value::Int64(val) => *val,
Value::Int32(val) => *val as i64,
Value::Int16(val) => *val as i64,
_ => Err(errors::invalid_value(type_name::<Self>(), val))?,
};
buf.reserve(8);
Expand Down Expand Up @@ -410,8 +413,9 @@ impl Codec for Float64 {
RawCodec::decode(buf).map(Value::Float64)
}
fn encode(&self, buf: &mut BytesMut, val: &Value) -> Result<(), EncodeError> {
let &val = match val {
Value::Float64(val) => val,
let val = match val {
Value::Float64(val) => *val,
Value::Float32(val) => *val as f64,
_ => Err(errors::invalid_value(type_name::<Self>(), val))?,
};
buf.reserve(8);
Expand Down