mirror of
https://github.com/tribufu/rust-gamedig
synced 2026-06-01 09:42:41 +00:00
[Crate] Convert pub(crate) to pub in buffer as its not exported externally
This commit is contained in:
parent
5aa2ce99ec
commit
b509a6dcc4
1 changed files with 15 additions and 15 deletions
|
|
@ -11,7 +11,7 @@ use std::{convert::TryInto, marker::PhantomData};
|
||||||
/// bind it to a specific byte order (BigEndian or LittleEndian).
|
/// bind it to a specific byte order (BigEndian or LittleEndian).
|
||||||
///
|
///
|
||||||
/// The byte order is defined by the `B: ByteOrder` generic parameter.
|
/// The byte order is defined by the `B: ByteOrder` generic parameter.
|
||||||
pub(crate) struct Buffer<'a, B: ByteOrder> {
|
pub struct Buffer<'a, B: ByteOrder> {
|
||||||
/// The byte slice that the buffer reads from.
|
/// The byte slice that the buffer reads from.
|
||||||
data: &'a [u8],
|
data: &'a [u8],
|
||||||
/// The cursor marking our current position in the buffer.
|
/// The cursor marking our current position in the buffer.
|
||||||
|
|
@ -29,7 +29,7 @@ impl<'a, B: ByteOrder> Buffer<'a, B> {
|
||||||
/// # Arguments
|
/// # Arguments
|
||||||
///
|
///
|
||||||
/// * `data` - A byte slice that the buffer will read from.
|
/// * `data` - A byte slice that the buffer will read from.
|
||||||
pub(crate) const fn new(data: &'a [u8]) -> Self {
|
pub const fn new(data: &'a [u8]) -> Self {
|
||||||
Self {
|
Self {
|
||||||
data,
|
data,
|
||||||
cursor: 0,
|
cursor: 0,
|
||||||
|
|
@ -37,18 +37,18 @@ impl<'a, B: ByteOrder> Buffer<'a, B> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) const fn current_position(&self) -> usize { self.cursor }
|
pub const fn current_position(&self) -> usize { self.cursor }
|
||||||
|
|
||||||
/// Returns the length of the remaining bytes from the current cursor
|
/// Returns the length of the remaining bytes from the current cursor
|
||||||
/// position.
|
/// position.
|
||||||
pub(crate) const fn remaining_length(&self) -> usize { self.data.len() - self.cursor }
|
pub const fn remaining_length(&self) -> usize { self.data.len() - self.cursor }
|
||||||
|
|
||||||
/// Returns the length of the buffer data.
|
/// Returns the length of the buffer data.
|
||||||
pub(crate) const fn data_length(&self) -> usize { self.data.len() }
|
pub const fn data_length(&self) -> usize { self.data.len() }
|
||||||
|
|
||||||
// Added for legacy support just for the refactoring
|
// Added for legacy support just for the refactoring
|
||||||
// Not Tested
|
// Not Tested
|
||||||
pub(crate) fn remaining_bytes(&self) -> &[u8] { &self.data[self.cursor ..] }
|
pub fn remaining_bytes(&self) -> &[u8] { &self.data[self.cursor ..] }
|
||||||
|
|
||||||
/// Moves the cursor forward or backward by a specified offset.
|
/// Moves the cursor forward or backward by a specified offset.
|
||||||
///
|
///
|
||||||
|
|
@ -61,7 +61,7 @@ impl<'a, B: ByteOrder> Buffer<'a, B> {
|
||||||
///
|
///
|
||||||
/// Returns a `BufferError` if the attempted move would position the cursor
|
/// Returns a `BufferError` if the attempted move would position the cursor
|
||||||
/// out of bounds.
|
/// out of bounds.
|
||||||
pub(crate) fn move_cursor(&mut self, offset: isize) -> GDResult<()> {
|
pub fn move_cursor(&mut self, offset: isize) -> GDResult<()> {
|
||||||
// Compute the new cursor position by adding the offset to the current cursor
|
// Compute the new cursor position by adding the offset to the current cursor
|
||||||
// position. The checked_add method is used for safe addition,
|
// position. The checked_add method is used for safe addition,
|
||||||
// preventing overflow and underflow.
|
// preventing overflow and underflow.
|
||||||
|
|
@ -99,7 +99,7 @@ impl<'a, B: ByteOrder> Buffer<'a, B> {
|
||||||
///
|
///
|
||||||
/// Returns a `BufferError` if there is not enough data remaining in the
|
/// Returns a `BufferError` if there is not enough data remaining in the
|
||||||
/// buffer to read a value of type `T`.
|
/// buffer to read a value of type `T`.
|
||||||
pub(crate) fn read<T: Sized + BufferRead<B>>(&mut self) -> GDResult<T> {
|
pub fn read<T: Sized + BufferRead<B>>(&mut self) -> GDResult<T> {
|
||||||
// Get the size of `T` in bytes.
|
// Get the size of `T` in bytes.
|
||||||
let size = std::mem::size_of::<T>();
|
let size = std::mem::size_of::<T>();
|
||||||
// Calculate remaining length of the buffer.
|
// Calculate remaining length of the buffer.
|
||||||
|
|
@ -143,7 +143,7 @@ impl<'a, B: ByteOrder> Buffer<'a, B> {
|
||||||
/// # Errors
|
/// # Errors
|
||||||
///
|
///
|
||||||
/// Returns a `BufferError` if there is an error decoding the string.
|
/// Returns a `BufferError` if there is an error decoding the string.
|
||||||
pub(crate) fn read_string<D: StringDecoder>(&mut self, until: Option<D::Delimiter>) -> GDResult<String> {
|
pub fn read_string<D: StringDecoder>(&mut self, until: Option<D::Delimiter>) -> GDResult<String> {
|
||||||
// Slice the data array from the current cursor position to the end.
|
// Slice the data array from the current cursor position to the end.
|
||||||
let data_slice = &self.data[self.cursor ..];
|
let data_slice = &self.data[self.cursor ..];
|
||||||
|
|
||||||
|
|
@ -171,7 +171,7 @@ impl<'a, B: ByteOrder> Buffer<'a, B> {
|
||||||
/// type `Output`.
|
/// type `Output`.
|
||||||
///
|
///
|
||||||
/// The associated type `Output` must implement the `ByteOrder` trait.
|
/// The associated type `Output` must implement the `ByteOrder` trait.
|
||||||
pub(crate) trait SwitchEndian {
|
pub trait SwitchEndian {
|
||||||
type Output: ByteOrder;
|
type Output: ByteOrder;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -204,7 +204,7 @@ impl<'a, B: SwitchEndian + ByteOrder> Buffer<'a, B> {
|
||||||
/// # Parameters
|
/// # Parameters
|
||||||
///
|
///
|
||||||
/// * `size`: The size of the chunk to be taken from the original buffer.
|
/// * `size`: The size of the chunk to be taken from the original buffer.
|
||||||
pub(crate) fn switch_endian_chunk(&mut self, size: usize) -> GDResult<Buffer<'a, B::Output>> {
|
pub fn switch_endian_chunk(&mut self, size: usize) -> GDResult<Buffer<'a, B::Output>> {
|
||||||
let old_cursor = self.cursor;
|
let old_cursor = self.cursor;
|
||||||
self.move_cursor(size as isize)?;
|
self.move_cursor(size as isize)?;
|
||||||
|
|
||||||
|
|
@ -221,7 +221,7 @@ impl<'a, B: SwitchEndian + ByteOrder> Buffer<'a, B> {
|
||||||
///
|
///
|
||||||
/// Implementors of this trait provide a method for reading their type from a
|
/// Implementors of this trait provide a method for reading their type from a
|
||||||
/// byte buffer with a specific byte order.
|
/// byte buffer with a specific byte order.
|
||||||
pub(crate) trait BufferRead<B: ByteOrder>: Sized {
|
pub trait BufferRead<B: ByteOrder>: Sized {
|
||||||
fn read_from_buffer(data: &[u8]) -> GDResult<Self>;
|
fn read_from_buffer(data: &[u8]) -> GDResult<Self>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -297,7 +297,7 @@ impl_buffer_read!(f64, read_f64);
|
||||||
///
|
///
|
||||||
/// This trait should be implemented by types that can decode strings from a
|
/// This trait should be implemented by types that can decode strings from a
|
||||||
/// byte buffer with a specific byte order and delimiter.
|
/// byte buffer with a specific byte order and delimiter.
|
||||||
pub(crate) trait StringDecoder {
|
pub trait StringDecoder {
|
||||||
/// The type of the delimiter used by the decoder.
|
/// The type of the delimiter used by the decoder.
|
||||||
type Delimiter: AsRef<[u8]>;
|
type Delimiter: AsRef<[u8]>;
|
||||||
|
|
||||||
|
|
@ -322,7 +322,7 @@ pub(crate) trait StringDecoder {
|
||||||
/// A decoder for UTF-8 encoded strings.
|
/// A decoder for UTF-8 encoded strings.
|
||||||
///
|
///
|
||||||
/// This decoder uses a single null byte (`0x00`) as the default delimiter.
|
/// This decoder uses a single null byte (`0x00`) as the default delimiter.
|
||||||
pub(crate) struct Utf8Decoder;
|
pub struct Utf8Decoder;
|
||||||
|
|
||||||
impl StringDecoder for Utf8Decoder {
|
impl StringDecoder for Utf8Decoder {
|
||||||
type Delimiter = [u8; 1];
|
type Delimiter = [u8; 1];
|
||||||
|
|
@ -368,7 +368,7 @@ impl StringDecoder for Utf8Decoder {
|
||||||
/// # Type Parameters
|
/// # Type Parameters
|
||||||
///
|
///
|
||||||
/// * `B` - The byte order to use when decoding the string.
|
/// * `B` - The byte order to use when decoding the string.
|
||||||
pub(crate) struct Utf16Decoder<B: ByteOrder> {
|
pub struct Utf16Decoder<B: ByteOrder> {
|
||||||
_marker: PhantomData<B>,
|
_marker: PhantomData<B>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue