Skip to content

Commit

Permalink
feat[rust]: compatible protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysunxiao committed Jul 21, 2024
1 parent f252aec commit 2a3ba99
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ private String protocol_read_deserialization(ProtocolRegistration registration)
var field = fields[i];
var fieldRegistration = fieldRegistrations[i];
if (field.isAnnotationPresent(Compatible.class)) {
rustBuilder.append("if (buffer.compatibleRead(beforeReadIndex, length)) {").append(LS);
rustBuilder.append("if buffer.compatibleRead(beforeReadIndex, length) {").append(LS);
var compatibleReadObject = rustSerializer(fieldRegistration.serializer()).readObject(rustBuilder, 1, field, fieldRegistration);
rustBuilder.append(TAB).append(StringUtils.format("packet.{} = {};", field.getName(), compatibleReadObject)).append(LS);
rustBuilder.append("}").append(LS);
Expand Down
4 changes: 4 additions & 0 deletions protocol/src/main/resources/rust/byte_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ impl IByteBuffer for ByteBuffer {
}
}

fn compatibleRead(&mut self, beforeReadIndex: i32, length: i32) -> bool{
return length != -1 && self.getReadOffset() < length + beforeReadIndex;
}

fn getBuffer(&self) -> &Vec<i8> {
return &self.buffer;
}
Expand Down
1 change: 1 addition & 0 deletions protocol/src/main/resources/rust/i_byte_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub trait IPacket {
#[allow(unused_parens)]
pub trait IByteBuffer {
fn adjustPadding(&mut self, predictionLength: i32, beforeWriteIndex: i32);
fn compatibleRead(&mut self, beforeReadIndex: i32, length: i32) -> bool;
fn getBuffer(&self) -> &Vec<i8>;
fn getWriteOffset(&self) -> i32;
fn setWriteOffset(&mut self, writeIndex: i32);
Expand Down

0 comments on commit 2a3ba99

Please sign in to comment.