Trait valence::protocol::Encode

pub trait Encode {
    // Required method
    fn encode(&self, w: impl Write) -> Result<(), Error>;

    // Provided method
    fn encode_slice(slice: &[Self], w: impl Write) -> Result<(), Error>
       where Self: Sized { ... }
}
Expand description

The Encode trait allows objects to be written to the Minecraft protocol. It is the inverse of Decode.

§Deriving

This trait can be implemented automatically for structs and enums by using the Encode derive macro. All components of the type must implement Encode. Components are encoded in the order they appear in the type definition.

For enums, the variant to encode is marked by a leading VarInt discriminant (tag). The discriminant value can be changed using the #[packet(tag = ...)] attribute on the variant in question. Discriminant values are assigned to variants using rules similar to regular enum discriminants.

use valence_protocol::Encode;

#[derive(Encode)]
struct MyStruct<'a> {
    first: i32,
    second: &'a str,
    third: [f64; 3],
}

#[derive(Encode)]
enum MyEnum {
    First,  // tag = 0
    Second, // tag = 1
    #[packet(tag = 25)]
    Third, // tag = 25
    Fourth, // tag = 26
}

let value = MyStruct {
    first: 10,
    second: "hello",
    third: [1.5, 3.14, 2.718],
};

let mut buf = vec![];
value.encode(&mut buf).unwrap();

println!("{buf:?}");

Required Methods§

fn encode(&self, w: impl Write) -> Result<(), Error>

Writes this object to the provided writer.

If this type also implements Decode then successful calls to this function returning Ok(()) must always successfully decode using the data that was written to the writer. The exact number of bytes that were originally written must be consumed during the decoding.

Provided Methods§

fn encode_slice(slice: &[Self], w: impl Write) -> Result<(), Error>
where Self: Sized,

Like Encode::encode, except that a whole slice of values is encoded.

This method must be semantically equivalent to encoding every element of the slice in sequence with no leading length prefix (which is exactly what the default implementation does), but a more efficient implementation may be used.

This method is important for some types like u8 where the entire slice can be encoded in a single call to write_all. Because impl specialization is unavailable in stable Rust at the time of writing, we must make the slice specialization part of this trait.

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

§

impl Encode for bool

§

fn encode(&self, w: impl Write) -> Result<(), Error>

§

fn encode_slice(slice: &[bool], w: impl Write) -> Result<(), Error>

§

impl Encode for f32

§

fn encode(&self, w: impl Write) -> Result<(), Error>

§

impl Encode for f64

§

fn encode(&self, w: impl Write) -> Result<(), Error>

§

impl Encode for i8

§

fn encode(&self, w: impl Write) -> Result<(), Error>

§

fn encode_slice(slice: &[i8], w: impl Write) -> Result<(), Error>

§

impl Encode for i16

§

fn encode(&self, w: impl Write) -> Result<(), Error>

§

impl Encode for i32

§

fn encode(&self, w: impl Write) -> Result<(), Error>

§

impl Encode for i64

§

fn encode(&self, w: impl Write) -> Result<(), Error>

§

impl Encode for i128

§

fn encode(&self, w: impl Write) -> Result<(), Error>

§

impl Encode for str

§

fn encode(&self, w: impl Write) -> Result<(), Error>

§

impl Encode for u8

§

fn encode(&self, w: impl Write) -> Result<(), Error>

§

fn encode_slice(slice: &[u8], w: impl Write) -> Result<(), Error>

§

impl Encode for u16

§

fn encode(&self, w: impl Write) -> Result<(), Error>

§

impl Encode for u32

§

fn encode(&self, w: impl Write) -> Result<(), Error>

§

impl Encode for u64

§

fn encode(&self, w: impl Write) -> Result<(), Error>

§

impl Encode for u128

§

fn encode(&self, w: impl Write) -> Result<(), Error>

§

impl Encode for ()

§

fn encode(&self, _w: impl Write) -> Result<(), Error>

§

impl Encode for String

§

fn encode(&self, w: impl Write) -> Result<(), Error>

§

impl<'a, B> Encode for Cow<'a, B>
where B: ToOwned + Encode + ?Sized,

§

fn encode(&self, w: impl Write) -> Result<(), Error>

§

impl<A> Encode for (A,)
where A: Encode,

§

fn encode(&self, _w: impl Write) -> Result<(), Error>

§

impl<A, B> Encode for (A, B)
where A: Encode, B: Encode,

§

fn encode(&self, _w: impl Write) -> Result<(), Error>

§

impl<A, B, C> Encode for (A, B, C)
where A: Encode, B: Encode, C: Encode,

§

fn encode(&self, _w: impl Write) -> Result<(), Error>

§

impl<A, B, C, D> Encode for (A, B, C, D)
where A: Encode, B: Encode, C: Encode, D: Encode,

§

fn encode(&self, _w: impl Write) -> Result<(), Error>

§

impl<A, B, C, D, E> Encode for (A, B, C, D, E)
where A: Encode, B: Encode, C: Encode, D: Encode, E: Encode,

§

fn encode(&self, _w: impl Write) -> Result<(), Error>

§

impl<A, B, C, D, E, F> Encode for (A, B, C, D, E, F)
where A: Encode, B: Encode, C: Encode, D: Encode, E: Encode, F: Encode,

§

fn encode(&self, _w: impl Write) -> Result<(), Error>

§

impl<A, B, C, D, E, F, G> Encode for (A, B, C, D, E, F, G)
where A: Encode, B: Encode, C: Encode, D: Encode, E: Encode, F: Encode, G: Encode,

§

fn encode(&self, _w: impl Write) -> Result<(), Error>

§

impl<A, B, C, D, E, F, G, H> Encode for (A, B, C, D, E, F, G, H)
where A: Encode, B: Encode, C: Encode, D: Encode, E: Encode, F: Encode, G: Encode, H: Encode,

§

fn encode(&self, _w: impl Write) -> Result<(), Error>

§

impl<A, B, C, D, E, F, G, H, I> Encode for (A, B, C, D, E, F, G, H, I)
where A: Encode, B: Encode, C: Encode, D: Encode, E: Encode, F: Encode, G: Encode, H: Encode, I: Encode,

§

fn encode(&self, _w: impl Write) -> Result<(), Error>

§

impl<A, B, C, D, E, F, G, H, I, J> Encode for (A, B, C, D, E, F, G, H, I, J)
where A: Encode, B: Encode, C: Encode, D: Encode, E: Encode, F: Encode, G: Encode, H: Encode, I: Encode, J: Encode,

§

fn encode(&self, _w: impl Write) -> Result<(), Error>

§

impl<A, B, C, D, E, F, G, H, I, J, K> Encode for (A, B, C, D, E, F, G, H, I, J, K)
where A: Encode, B: Encode, C: Encode, D: Encode, E: Encode, F: Encode, G: Encode, H: Encode, I: Encode, J: Encode, K: Encode,

§

fn encode(&self, _w: impl Write) -> Result<(), Error>

§

impl<A, B, C, D, E, F, G, H, I, J, K, L> Encode for (A, B, C, D, E, F, G, H, I, J, K, L)
where A: Encode, B: Encode, C: Encode, D: Encode, E: Encode, F: Encode, G: Encode, H: Encode, I: Encode, J: Encode, K: Encode, L: Encode,

§

fn encode(&self, _w: impl Write) -> Result<(), Error>

§

impl<K, V> Encode for BTreeMap<K, V>
where K: Encode, V: Encode,

§

fn encode(&self, w: impl Write) -> Result<(), Error>

§

impl<K, V, S> Encode for HashMap<K, V, S>
where K: Encode, V: Encode,

§

fn encode(&self, w: impl Write) -> Result<(), Error>

§

impl<T> Encode for Option<T>
where T: Encode,

§

fn encode(&self, w: impl Write) -> Result<(), Error>

§

impl<T> Encode for &T
where T: Encode + ?Sized,

§

fn encode(&self, w: impl Write) -> Result<(), Error>

§

impl<T> Encode for &mut T
where T: Encode + ?Sized,

§

fn encode(&self, w: impl Write) -> Result<(), Error>

§

impl<T> Encode for [T]
where T: Encode,

§

fn encode(&self, w: impl Write) -> Result<(), Error>

§

impl<T> Encode for Box<T>
where T: Encode + ?Sized,

§

fn encode(&self, w: impl Write) -> Result<(), Error>

§

impl<T> Encode for BTreeSet<T>
where T: Encode,

§

fn encode(&self, w: impl Write) -> Result<(), Error>

§

impl<T> Encode for Rc<T>
where T: Encode + ?Sized,

§

fn encode(&self, w: impl Write) -> Result<(), Error>

§

impl<T> Encode for Arc<T>
where T: Encode + ?Sized,

§

fn encode(&self, w: impl Write) -> Result<(), Error>

§

impl<T> Encode for Vec<T>
where T: Encode,

§

fn encode(&self, w: impl Write) -> Result<(), Error>

§

impl<T, S> Encode for HashSet<T, S>
where T: Encode,

§

fn encode(&self, w: impl Write) -> Result<(), Error>

§

impl<T, const N: usize> Encode for [T; N]
where T: Encode,

Like tuples, fixed-length arrays are encoded and decoded without a VarInt length prefix.

§

fn encode(&self, w: impl Write) -> Result<(), Error>

Implementors§

§

impl Encode for BlockEntityKind

§

impl Encode for BoatKind

§

impl Encode for CatKind

§

impl Encode for FrogKind

§

impl Encode for PaintingKind

§

impl Encode for Pose

§

impl Encode for SnifferState

§

impl Encode for VillagerKind

§

impl Encode for VillagerProfession

§

impl Encode for BlockKind

§

impl Encode for Difficulty

§

impl Encode for Direction

§

impl Encode for GameMode

§

impl Encode for Hand

§

impl Encode for ItemKind

§

impl Encode for Particle

Encodes the particle without an ID.

§

impl Encode for HandshakeNextState

§

impl Encode for BossBarColor

§

impl Encode for BossBarDivision

§

impl Encode for MessageFilterType

§

impl Encode for ChatSuggestionsAction

§

impl Encode for ClickMode

§

impl Encode for ClientCommand

§

impl Encode for ChatMode

§

impl Encode for MainArm

§

impl Encode for Parser

§

impl Encode for StringArg

§

impl Encode for ClientStatusC2s

§

impl Encode for ResourcePackStatusC2s

§

impl Encode for UpdatePlayerAbilitiesC2s

§

impl Encode for GameEventKind

§

impl Encode for FeetOrEyes

§

impl Encode for IconType

§

impl Encode for WindowType

§

impl Encode for PlayerAction

§

impl Encode for EntityInteraction

§

impl Encode for RecipeBookId

§

impl Encode for ScoreboardPosition

§

impl Encode for ObjectiveRenderType

§

impl Encode for CraftingShapedCategory

§

impl Encode for valence::protocol::packets::play::team_s2c::Mode<'_>

§

impl Encode for TeamColor

§

impl Encode for UpdateCommandBlockMode

§

impl Encode for Action

§

impl Encode for Mirror

§

impl Encode for valence::protocol::packets::play::update_structure_block_c2s::Mode

§

impl Encode for Rotation

§

impl Encode for SoundCategory

§

impl Encode for SoundId<'_>

§

impl Encode for EulerAngle

§

impl Encode for VillagerData

§

impl Encode for DQuat

§

impl Encode for DVec2

§

impl Encode for DVec3

§

impl Encode for IVec3

§

impl Encode for Quat

§

impl Encode for Vec2

§

impl Encode for Vec3

§

impl Encode for Vec3A

§

impl Encode for Vec4

§

impl Encode for Compound

§

impl Encode for Uuid

§

impl Encode for PackedBlockPos

§

impl Encode for PackedChunkSectionPos

§

impl Encode for OptGameMode

§

impl Encode for LoginCompressionS2c

§

impl Encode for BossBarFlags

§

impl Encode for ChunkDeltaUpdateEntry

§

impl Encode for SlotChange

§

impl Encode for DisplayedSkinParts

§

impl Encode for Node

§

impl Encode for AttributeModifier

§

impl Encode for EquipmentEntry

§

impl Encode for valence::protocol::packets::play::entity_status_effect_s2c::Flags

§

impl Encode for LookAtEntity

§

impl Encode for PlayerAbilitiesFlags

§

impl Encode for PlayerInputFlags

§

impl Encode for PlayerPositionLookFlags

§

impl Encode for TradeOffer

§

impl Encode for Statistic

§

impl Encode for BlockBreakingProgressS2c

§

impl Encode for BlockEventS2c

§

impl Encode for BlockUpdateS2c

§

impl Encode for BoatPaddleStateC2s

§

impl Encode for BundleSplitterS2c

§

impl Encode for ButtonClickC2s

§

impl Encode for ChunkLoadDistanceS2c

§

impl Encode for ChunkRenderDistanceCenterS2c

§

impl Encode for ClearTitleS2c

§

impl Encode for ClientCommandC2s

§

impl Encode for CloseHandledScreenC2s

§

impl Encode for CloseScreenS2c

§

impl Encode for CommandTreeS2c

§

impl Encode for CooldownUpdateS2c

§

impl Encode for CreativeInventoryActionC2s

§

impl Encode for DamageTiltS2c

§

impl Encode for DifficultyS2c

§

impl Encode for EndCombatS2c

§

impl Encode for EnterCombatS2c

§

impl Encode for EntityAnimationS2c

§

impl Encode for EntityAttachS2c

§

impl Encode for EntityDamageS2c

§

impl Encode for EntityEquipmentUpdateS2c

§

impl Encode for EntityPositionS2c

§

impl Encode for EntitySetHeadYawS2c

§

impl Encode for EntitySpawnS2c

§

impl Encode for EntityStatusEffectS2c

§

impl Encode for EntityStatusS2c

§

impl Encode for EntityVelocityUpdateS2c

§

impl Encode for ExperienceBarUpdateS2c

§

impl Encode for ExperienceOrbSpawnS2c

§

impl Encode for FullC2s

§

impl Encode for GameStateChangeS2c

§

impl Encode for HandSwingC2s

§

impl Encode for HealthUpdateS2c

§

impl Encode for ItemPickupAnimationS2c

§

impl Encode for JigsawGeneratingC2s

§

impl Encode for KeepAliveC2s

§

impl Encode for KeepAliveS2c

§

impl Encode for LookAndOnGroundC2s

§

impl Encode for LookAtS2c

§

impl Encode for MapUpdateS2c<'_>

§

impl Encode for MessageAcknowledgmentC2s

§

impl Encode for MoveRelativeS2c

§

impl Encode for NbtQueryResponseS2c

§

impl Encode for OnGroundOnlyC2s

§

impl Encode for OpenHorseScreenS2c

§

impl Encode for OpenWrittenBookS2c

§

impl Encode for ParticleS2c<'_>

§

impl Encode for PickFromInventoryC2s

§

impl Encode for PlayPingS2c

§

impl Encode for PlayPongC2s

§

impl Encode for PlaySoundFromEntityS2c

§

impl Encode for PlayerAbilitiesS2c

§

impl Encode for PlayerActionC2s

§

impl Encode for PlayerActionResponseS2c

§

impl Encode for PlayerInputC2s

§

impl Encode for PlayerInteractBlockC2s

§

impl Encode for PlayerInteractEntityC2s

§

impl Encode for PlayerInteractItemC2s

§

impl Encode for PlayerPositionLookS2c

§

impl Encode for PlayerSpawnPositionS2c

§

impl Encode for PlayerSpawnS2c

§

impl Encode for PositionAndOnGroundC2s

§

impl Encode for QueryBlockNbtC2s

§

impl Encode for QueryEntityNbtC2s

§

impl Encode for RecipeCategoryOptionsC2s

§

impl Encode for RemoveEntityStatusEffectS2c

§

impl Encode for RotateAndMoveRelativeS2c

§

impl Encode for RotateS2c

§

impl Encode for ScreenHandlerPropertyUpdateS2c

§

impl Encode for SelectMerchantTradeC2s

§

impl Encode for SetCameraEntityS2c

§

impl Encode for SetTradeOffersS2c

§

impl Encode for SignEditorOpenS2c

§

impl Encode for SimulationDistanceS2c

§

impl Encode for SpectatorTeleportC2s

§

impl Encode for StatisticsS2c

§

impl Encode for StopSoundS2c<'_>

§

impl Encode for TeleportConfirmC2s

§

impl Encode for TitleFadeS2c

§

impl Encode for UnloadChunkS2c

§

impl Encode for UnlockRecipesS2c<'_>

§

impl Encode for UpdateBeaconC2s

§

impl Encode for UpdateDifficultyC2s

§

impl Encode for UpdateDifficultyLockC2s

§

impl Encode for UpdateSelectedSlotC2s

§

impl Encode for UpdateSelectedSlotS2c

§

impl Encode for VehicleMoveC2s

§

impl Encode for VehicleMoveS2c

§

impl Encode for WorldBorderCenterChangedS2c

§

impl Encode for WorldBorderInitializeS2c

§

impl Encode for WorldBorderInterpolateSizeS2c

§

impl Encode for WorldBorderSizeChangedS2c

§

impl Encode for WorldBorderWarningBlocksChangedS2c

§

impl Encode for WorldBorderWarningTimeChangedS2c

§

impl Encode for WorldEventS2c

§

impl Encode for WorldTimeUpdateS2c

§

impl Encode for CraftingShapedData<'_>

§

impl Encode for TeamFlags

§

impl Encode for UpdateCommandBlockFlags

§

impl Encode for valence::protocol::packets::play::update_structure_block_c2s::Flags

§

impl Encode for QueryPingC2s

§

impl Encode for QueryPongS2c

§

impl Encode for QueryRequestC2s

§

impl Encode for BlockPos

§

impl Encode for BlockState

§

impl Encode for ByteAngle

§

impl Encode for ChunkPos

§

impl Encode for ChunkSectionPos

§

impl Encode for ItemStack

§

impl Encode for RawBytes<'_>

§

impl Encode for Text

§

impl Encode for VarInt

§

impl Encode for VarLong

§

impl Encode for Velocity

§

impl<'a> Encode for BossBarAction<'a>

§

impl<'a> Encode for AdvancementTabC2s<'a>

§

impl<'a> Encode for ObjectiveMode<'a>

§

impl<'a> Encode for ScoreboardPlayerUpdateAction<'a>

§

impl<'a> Encode for RecipeData<'a>

§

impl<'a> Encode for HandshakeC2s<'a>

§

impl<'a> Encode for LoginDisconnectS2c<'a>

§

impl<'a> Encode for LoginHelloC2s<'a>

§

impl<'a> Encode for LoginHelloS2c<'a>

§

impl<'a> Encode for LoginKeyC2s<'a>

§

impl<'a> Encode for LoginQueryRequestS2c<'a>

§

impl<'a> Encode for LoginQueryResponseC2s<'a>

§

impl<'a> Encode for LoginSuccessS2c<'a>

§

impl<'a> Encode for AdvancementCriteria<'a>

§

impl<'a> Encode for AdvancementRequirements<'a>

§

impl<'a> Encode for MessageSignature<'a>

§

impl<'a> Encode for ChunkBiome<'a>

§

impl<'a> Encode for ChunkDataBlockEntity<'a>

§

impl<'a> Encode for CommandArgumentSignature<'a>

§

impl<'a> Encode for CommandSuggestionsMatch<'a>

§

impl<'a> Encode for AttributeProperty<'a>

§

impl<'a> Encode for Data<'a>

§

impl<'a> Encode for Icon<'a>

§

impl<'a> Encode for ChatData<'a>

§

impl<'a> Encode for BlockEntityUpdateS2c<'a>

§

impl<'a> Encode for BookUpdateC2s<'a>

§

impl<'a> Encode for BossBarS2c<'a>

§

impl<'a> Encode for ChatMessageC2s<'a>

§

impl<'a> Encode for ChatMessageS2c<'a>

§

impl<'a> Encode for ChatSuggestionsS2c<'a>

§

impl<'a> Encode for ChunkBiomeDataS2c<'a>

§

impl<'a> Encode for ChunkDataS2c<'a>

§

impl<'a> Encode for ChunkDeltaUpdateS2c<'a>

§

impl<'a> Encode for ClickSlotC2s<'a>

§

impl<'a> Encode for ClientSettingsC2s<'a>

§

impl<'a> Encode for CommandExecutionC2s<'a>

§

impl<'a> Encode for CommandSuggestionsS2c<'a>

§

impl<'a> Encode for CraftFailedResponseS2c<'a>

§

impl<'a> Encode for CraftRequestC2s<'a>

§

impl<'a> Encode for CustomPayloadC2s<'a>

§

impl<'a> Encode for CustomPayloadS2c<'a>

§

impl<'a> Encode for DeathMessageS2c<'a>

§

impl<'a> Encode for DisconnectS2c<'a>

§

impl<'a> Encode for EntitiesDestroyS2c<'a>

§

impl<'a> Encode for EntityAttributesS2c<'a>

§

impl<'a> Encode for EntityPassengersSetS2c<'a>

§

impl<'a> Encode for EntityTrackerUpdateS2c<'a>

§

impl<'a> Encode for ExplosionS2c<'a>

§

impl<'a> Encode for FeaturesS2c<'a>

§

impl<'a> Encode for GameJoinS2c<'a>

§

impl<'a> Encode for GameMessageS2c<'a>

§

impl<'a> Encode for InventoryS2c<'a>

§

impl<'a> Encode for LightUpdateS2c<'a>

§

impl<'a> Encode for OpenScreenS2c<'a>

§

impl<'a> Encode for OverlayMessageS2c<'a>

§

impl<'a> Encode for PlaySoundS2c<'a>

§

impl<'a> Encode for PlayerListHeaderS2c<'a>

§

impl<'a> Encode for PlayerListS2c<'a>

§

impl<'a> Encode for PlayerRemoveS2c<'a>

§

impl<'a> Encode for PlayerRespawnS2c<'a>

§

impl<'a> Encode for PlayerSessionC2s<'a>

§

impl<'a> Encode for ProfilelessChatMessageS2c<'a>

§

impl<'a> Encode for RecipeBookDataC2s<'a>

§

impl<'a> Encode for RemoveMessageS2c<'a>

§

impl<'a> Encode for RenameItemC2s<'a>

§

impl<'a> Encode for RequestCommandCompletionsC2s<'a>

§

impl<'a> Encode for ResourcePackSendS2c<'a>

§

impl<'a> Encode for ScoreboardDisplayS2c<'a>

§

impl<'a> Encode for ScoreboardObjectiveUpdateS2c<'a>

§

impl<'a> Encode for ScoreboardPlayerUpdateS2c<'a>

§

impl<'a> Encode for ScreenHandlerSlotUpdateS2c<'a>

§

impl<'a> Encode for SelectAdvancementTabS2c<'a>

§

impl<'a> Encode for ServerMetadataS2c<'a>

§

impl<'a> Encode for SubtitleS2c<'a>

§

impl<'a> Encode for SynchronizeRecipesS2c<'a>

§

impl<'a> Encode for SynchronizeTagsS2c<'a>

§

impl<'a> Encode for TeamS2c<'a>

§

impl<'a> Encode for TitleS2c<'a>

§

impl<'a> Encode for UpdateCommandBlockC2s<'a>

§

impl<'a> Encode for UpdateCommandBlockMinecartC2s<'a>

§

impl<'a> Encode for UpdateJigsawC2s<'a>

§

impl<'a> Encode for UpdateSignC2s<'a>

§

impl<'a> Encode for UpdateStructureBlockC2s<'a>

§

impl<'a> Encode for Recipe<'a>

§

impl<'a> Encode for QueryResponseS2c<'a>

§

impl<'a> Encode for GlobalPos<'a>

§

impl<'a, AM> Encode for GenericAdvancementUpdateS2c<'a, AM>
where AM: 'a + Encode,

§

impl<'a, I> Encode for Advancement<'a, I>
where I: Encode,

§

impl<I> Encode for AdvancementDisplay<'_, I>
where I: Encode,

§

impl<S> Encode for Property<S>
where S: Encode,

§

impl<S> Encode for Ident<S>
where S: Encode,

§

impl<T, const MAX_LEN: usize> Encode for Bounded<&[T], MAX_LEN>
where T: Encode,

§

impl<T, const N: usize> Encode for FixedArray<T, N>
where T: Encode,

§

impl<const BIT_COUNT: usize, const BYTE_COUNT: usize> Encode for FixedBitSet<BIT_COUNT, BYTE_COUNT>

§

impl<const MAX_BYTES: usize> Encode for Bounded<RawBytes<'_>, MAX_BYTES>

Raises an encoding error if the inner slice is longer than MAX_BYTES.

§

impl<const MAX_CHARS: usize> Encode for Bounded<&str, MAX_CHARS>

§

impl<const MAX_CHARS: usize> Encode for Bounded<String, MAX_CHARS>