NetrexMC / Protocol

Minecraft Protocol Library
9 stars 1 forks source link

Utilize Rust Macros #5

Open john-bv opened 3 years ago

john-bv commented 3 years ago

Issue / Question: We should utilize rust Macros.

Relevance: Rust macros would save us time when writing protocol implementation, for instance, instead of repeatedly writing implementations for ProtocolEncoder/Decoder we can utilize macros, which will be able to do this for us.

Solution:

#[mcpe_packet(1)]
pub struct LoginPacket {
    pub protocol: u8;
    pub data: BinaryStream;
}

or more advanced:

#[derive(encoder, decoder)]
pub struct BehaviorPack {
    ... props
}
#[mcpe_packet(7, "custom")]
pub struct ResourcePacksInfo {
    pub required: bool,
    pub has_scripts: bool,
    pub bpacks: Vec<BehaviorPack>
}
umaYnit commented 3 years ago

This looks like a fun issue :smile: mind if I give this a go?

john-bv commented 3 years ago

go for it

umaYnit commented 2 years ago

should we change ProtocolEncoder trait to

pub trait ProtocolEncoder {
     fn write(&self, dst: &mut BinaryStream);
}

so that some memory copy can be avoided

john-bv commented 2 years ago

Sure, I feel like this won't hurt anything. This was originally recommended by @buchwasa for raknet, which we decided against just because of how often we need to make new streams in raknet.