Skip to content

Commit

Permalink
nbt edcode glob cx
Browse files Browse the repository at this point in the history
  • Loading branch information
0123456789-jpg committed Mar 15, 2024
1 parent 5deeed5 commit 526a148
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
2 changes: 2 additions & 0 deletions crates/core/global-cx/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ maintenance = { status = "passively-maintained" }
[dependencies]

[features]
default = ["std"]
std = []

[lints]
workspace = true
64 changes: 64 additions & 0 deletions crates/core/global-cx/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

#![no_std]

#[cfg(feature = "std")]
extern crate std;

/// Marker trait for global contexts.
pub trait GlobalContext: Sized + 'static {}

Expand All @@ -27,3 +30,64 @@ pub trait ProvideNbtTy: GlobalContext {
/// [`i64`] array type.
type LongArray;
}

#[cfg(feature = "std")]
/// NBT `edcode`-ing related marker traits.
pub mod nbt_edcode {
use crate::ProvideNbtTy;

/// Marker trait for global contexts that can write nbt tags to a [`std::io::Write`] object.
pub trait WriteNbt<T>: ProvideNbtTy {
/// Error type.
type Error;

/// Function that performs writing operation.
/// # Errors
/// I/O errors.
fn write_nbt<W>(value: T, writer: W) -> Result<(), Self::Error>
where
W: std::io::Write;
}

/// Marker trait for global contexts that can read nbt tags from a [`std::io::Read`] object.
pub trait ReadNbt<T>: ProvideNbtTy {
/// Error type.
type Error;

/// Function that performs reading operation.
/// # Errors
/// I/O errors.
fn read_nbt<R>(reader: R) -> Result<T, Self::Error>
where
R: std::io::Read;
}

/// Marker trait for global contexts that can update existing nbt tags from a [`std::io::Read`] object.
pub trait UpdateNbt<T: ?Sized>: ProvideNbtTy {
/// Error type.
type Error;

/// Function that performs updating operation.
/// # Errors
/// I/O errors.
fn update_nbt<R>(value: &mut T, reader: R) -> Result<(), Self::Error>
where
R: std::io::Read;
}

impl<T, Cx> UpdateNbt<T> for Cx
where
Cx: ReadNbt<T>,
{
type Error = <Self as ReadNbt<T>>::Error;

#[inline]
fn update_nbt<R>(value: &mut T, reader: R) -> Result<(), Self::Error>
where
R: std::io::Read,
{
*value = Self::read_nbt(reader)?;
Ok(())
}
}
}

0 comments on commit 526a148

Please sign in to comment.