diff options
| author | Valentin Popov <valentin@popov.link> | 2026-02-10 11:26:49 +0300 |
|---|---|---|
| committer | Valentin Popov <valentin@popov.link> | 2026-02-10 11:26:49 +0300 |
| commit | ce6e30f7272fd0c064ef52ac85cad1c0f05fd323 (patch) | |
| tree | b493ba02b81a8a4759f44560c1fd5951b84428e1 /crates/common/src/lib.rs | |
| parent | 4af183ad74bfaafa0dc9db8116d361582debe536 (diff) | |
| download | fparkan-ce6e30f7272fd0c064ef52ac85cad1c0f05fd323.tar.xz fparkan-ce6e30f7272fd0c064ef52ac85cad1c0f05fd323.zip | |
feat: добавить библиотеку common с ресурсами и буферами вывода; обновить зависимости в nres и rsli
Diffstat (limited to 'crates/common/src/lib.rs')
| -rw-r--r-- | crates/common/src/lib.rs | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/crates/common/src/lib.rs b/crates/common/src/lib.rs new file mode 100644 index 0000000..bb9e778 --- /dev/null +++ b/crates/common/src/lib.rs @@ -0,0 +1,43 @@ +use std::io; + +/// Resource payload that can be either borrowed from mapped bytes or owned. +#[derive(Clone, Debug)] +pub enum ResourceData<'a> { + Borrowed(&'a [u8]), + Owned(Vec<u8>), +} + +impl<'a> ResourceData<'a> { + pub fn as_slice(&self) -> &[u8] { + match self { + Self::Borrowed(slice) => slice, + Self::Owned(buf) => buf.as_slice(), + } + } + + pub fn into_owned(self) -> Vec<u8> { + match self { + Self::Borrowed(slice) => slice.to_vec(), + Self::Owned(buf) => buf, + } + } +} + +impl AsRef<[u8]> for ResourceData<'_> { + fn as_ref(&self) -> &[u8] { + self.as_slice() + } +} + +/// Output sink used by `read_into`/`load_into` APIs. +pub trait OutputBuffer { + fn write_exact(&mut self, data: &[u8]) -> io::Result<()>; +} + +impl OutputBuffer for Vec<u8> { + fn write_exact(&mut self, data: &[u8]) -> io::Result<()> { + self.clear(); + self.extend_from_slice(data); + Ok(()) + } +} |
