mirror of
https://github.com/italicsjenga/valence.git
synced 2024-12-23 14:31:30 +11:00
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:
parent
4a541a907a
commit
e933fd69f5
|
@ -31,7 +31,7 @@ use valence_protocol::packet::s2c::login::{
|
|||
LoginCompressionS2c, LoginDisconnectS2c, LoginHelloS2c, LoginQueryRequestS2c, LoginSuccessS2c,
|
||||
};
|
||||
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::types::Property;
|
||||
use valence_protocol::username::Username;
|
||||
|
|
|
@ -95,7 +95,7 @@ pub mod ident;
|
|||
mod impls;
|
||||
pub mod item;
|
||||
pub mod packet;
|
||||
pub mod raw_bytes;
|
||||
pub mod raw;
|
||||
pub mod sound;
|
||||
pub mod text;
|
||||
pub mod tracked_data;
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use crate::raw_bytes::RawBytes;
|
||||
use crate::raw::RawBytes;
|
||||
use crate::var_int::VarInt;
|
||||
use crate::{Decode, Encode};
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use crate::ident::Ident;
|
||||
use crate::raw_bytes::RawBytes;
|
||||
use crate::raw::RawBytes;
|
||||
use crate::{Decode, Encode};
|
||||
|
||||
#[derive(Copy, Clone, Debug, Encode, Decode)]
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use crate::ident::Ident;
|
||||
use crate::raw_bytes::RawBytes;
|
||||
use crate::raw::RawBytes;
|
||||
use crate::var_int::VarInt;
|
||||
use crate::{Decode, Encode};
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use crate::ident::Ident;
|
||||
use crate::raw_bytes::RawBytes;
|
||||
use crate::raw::RawBytes;
|
||||
use crate::{Decode, Encode};
|
||||
|
||||
#[derive(Copy, Clone, Debug, Encode, Decode)]
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use crate::raw_bytes::RawBytes;
|
||||
use crate::raw::RawBytes;
|
||||
use crate::var_int::VarInt;
|
||||
use crate::{Decode, Encode};
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
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
|
||||
/// without any length prefix or metadata.
|
||||
|
@ -10,7 +11,7 @@ use crate::{Decode, Encode, Result};
|
|||
///
|
||||
/// [encoding]: Encode
|
||||
/// [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]);
|
||||
|
||||
impl Encode for RawBytes<'_> {
|
||||
|
@ -21,9 +22,7 @@ impl Encode for RawBytes<'_> {
|
|||
|
||||
impl<'a> Decode<'a> for RawBytes<'a> {
|
||||
fn decode(r: &mut &'a [u8]) -> Result<Self> {
|
||||
let slice = *r;
|
||||
*r = &[];
|
||||
Ok(Self(slice))
|
||||
Ok(Self(mem::take(r)))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -38,3 +37,26 @@ impl<'a> From<RawBytes<'a>> for &'a [u8] {
|
|||
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)))
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue