Add RawPacket (#276)

## Description

Adds a `RawPacket` type to `valence_protocol`. This type simply reads
and writes data to a slice, analogous to the `RawBytes` type in the same
module.

## Test Plan

Steps:
1. `cargo t -p valence_protocol`
This commit is contained in:
Ryan Johnson 2023-03-07 20:15:30 -08:00 committed by GitHub
parent 4a541a907a
commit e933fd69f5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 34 additions and 12 deletions

View file

@ -31,7 +31,7 @@ use valence_protocol::packet::s2c::login::{
LoginCompressionS2c, LoginDisconnectS2c, LoginHelloS2c, LoginQueryRequestS2c, LoginSuccessS2c, LoginCompressionS2c, LoginDisconnectS2c, LoginHelloS2c, LoginQueryRequestS2c, LoginSuccessS2c,
}; };
use valence_protocol::packet::s2c::status::{QueryPongS2c, QueryResponseS2c}; use valence_protocol::packet::s2c::status::{QueryPongS2c, QueryResponseS2c};
use valence_protocol::raw_bytes::RawBytes; use valence_protocol::raw::RawBytes;
use valence_protocol::text::Text; use valence_protocol::text::Text;
use valence_protocol::types::Property; use valence_protocol::types::Property;
use valence_protocol::username::Username; use valence_protocol::username::Username;

View file

@ -95,7 +95,7 @@ pub mod ident;
mod impls; mod impls;
pub mod item; pub mod item;
pub mod packet; pub mod packet;
pub mod raw_bytes; pub mod raw;
pub mod sound; pub mod sound;
pub mod text; pub mod text;
pub mod tracked_data; pub mod tracked_data;

View file

@ -1,4 +1,4 @@
use crate::raw_bytes::RawBytes; use crate::raw::RawBytes;
use crate::var_int::VarInt; use crate::var_int::VarInt;
use crate::{Decode, Encode}; use crate::{Decode, Encode};

View file

@ -1,5 +1,5 @@
use crate::ident::Ident; use crate::ident::Ident;
use crate::raw_bytes::RawBytes; use crate::raw::RawBytes;
use crate::{Decode, Encode}; use crate::{Decode, Encode};
#[derive(Copy, Clone, Debug, Encode, Decode)] #[derive(Copy, Clone, Debug, Encode, Decode)]

View file

@ -1,5 +1,5 @@
use crate::ident::Ident; use crate::ident::Ident;
use crate::raw_bytes::RawBytes; use crate::raw::RawBytes;
use crate::var_int::VarInt; use crate::var_int::VarInt;
use crate::{Decode, Encode}; use crate::{Decode, Encode};

View file

@ -1,5 +1,5 @@
use crate::ident::Ident; use crate::ident::Ident;
use crate::raw_bytes::RawBytes; use crate::raw::RawBytes;
use crate::{Decode, Encode}; use crate::{Decode, Encode};
#[derive(Copy, Clone, Debug, Encode, Decode)] #[derive(Copy, Clone, Debug, Encode, Decode)]

View file

@ -1,4 +1,4 @@
use crate::raw_bytes::RawBytes; use crate::raw::RawBytes;
use crate::var_int::VarInt; use crate::var_int::VarInt;
use crate::{Decode, Encode}; use crate::{Decode, Encode};

View file

@ -1,6 +1,7 @@
use std::io::Write; use std::io::Write;
use std::mem;
use crate::{Decode, Encode, Result}; use crate::{Decode, Encode, Packet, Result};
/// While [encoding], the contained slice is written directly to the output /// While [encoding], the contained slice is written directly to the output
/// without any length prefix or metadata. /// without any length prefix or metadata.
@ -10,7 +11,7 @@ use crate::{Decode, Encode, Result};
/// ///
/// [encoding]: Encode /// [encoding]: Encode
/// [decoding]: Decode /// [decoding]: Decode
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Debug)]
pub struct RawBytes<'a>(pub &'a [u8]); pub struct RawBytes<'a>(pub &'a [u8]);
impl Encode for RawBytes<'_> { impl Encode for RawBytes<'_> {
@ -21,9 +22,7 @@ impl Encode for RawBytes<'_> {
impl<'a> Decode<'a> for RawBytes<'a> { impl<'a> Decode<'a> for RawBytes<'a> {
fn decode(r: &mut &'a [u8]) -> Result<Self> { fn decode(r: &mut &'a [u8]) -> Result<Self> {
let slice = *r; Ok(Self(mem::take(r)))
*r = &[];
Ok(Self(slice))
} }
} }
@ -38,3 +37,26 @@ impl<'a> From<RawBytes<'a>> for &'a [u8] {
value.0 value.0
} }
} }
/// A fake [`Packet`] which simply reads all data into a slice, or writes all
/// data from a slice. The packet ID is included in the slice.
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Debug)]
pub struct RawPacket<'a>(pub &'a [u8]);
impl<'a> Packet<'a> for RawPacket<'a> {
fn packet_id(&self) -> i32 {
-1
}
fn packet_name(&self) -> &str {
"RawPacket"
}
fn encode_packet(&self, mut w: impl Write) -> Result<()> {
Ok(w.write_all(self.0)?)
}
fn decode_packet(r: &mut &'a [u8]) -> Result<Self> {
Ok(Self(mem::take(r)))
}
}