summaryrefslogtreecommitdiff
path: root/crates/typst-library/src/loading
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2025-01-08 11:57:56 +0100
committerGitHub <noreply@github.com>2025-01-08 10:57:56 +0000
commitdacd6acd5e73d35c6e7a7a3b144f16ae70d03daa (patch)
treeca75ce7d4d5365fbe2424a356cefe9cb223d429f /crates/typst-library/src/loading
parent0a374d238016c0101d11cbc3f4bc621f3895ad36 (diff)
More flexible and efficient `Bytes` representation (#5670)
Diffstat (limited to 'crates/typst-library/src/loading')
-rw-r--r--crates/typst-library/src/loading/cbor.rs2
-rw-r--r--crates/typst-library/src/loading/mod.rs24
2 files changed, 12 insertions, 14 deletions
diff --git a/crates/typst-library/src/loading/cbor.rs b/crates/typst-library/src/loading/cbor.rs
index 977059c3..a03e5c99 100644
--- a/crates/typst-library/src/loading/cbor.rs
+++ b/crates/typst-library/src/loading/cbor.rs
@@ -55,7 +55,7 @@ impl cbor {
let Spanned { v: value, span } = value;
let mut res = Vec::new();
ciborium::into_writer(&value, &mut res)
- .map(|_| res.into())
+ .map(|_| Bytes::new(res))
.map_err(|err| eco_format!("failed to encode value as CBOR ({err})"))
.at(span)
}
diff --git a/crates/typst-library/src/loading/mod.rs b/crates/typst-library/src/loading/mod.rs
index ae74df86..120b3e3a 100644
--- a/crates/typst-library/src/loading/mod.rs
+++ b/crates/typst-library/src/loading/mod.rs
@@ -56,15 +56,22 @@ pub enum Readable {
impl Readable {
pub fn as_slice(&self) -> &[u8] {
match self {
- Readable::Bytes(v) => v,
- Readable::Str(v) => v.as_bytes(),
+ Self::Bytes(v) => v,
+ Self::Str(v) => v.as_bytes(),
}
}
pub fn as_str(&self) -> Option<&str> {
match self {
- Readable::Str(v) => Some(v.as_str()),
- Readable::Bytes(v) => std::str::from_utf8(v).ok(),
+ Self::Str(v) => Some(v.as_str()),
+ Self::Bytes(v) => std::str::from_utf8(v).ok(),
+ }
+ }
+
+ pub fn into_bytes(self) -> Bytes {
+ match self {
+ Self::Bytes(v) => v,
+ Self::Str(v) => Bytes::from_string(v),
}
}
}
@@ -78,12 +85,3 @@ cast! {
v: Str => Self::Str(v),
v: Bytes => Self::Bytes(v),
}
-
-impl From<Readable> for Bytes {
- fn from(value: Readable) -> Self {
- match value {
- Readable::Bytes(v) => v,
- Readable::Str(v) => v.as_bytes().into(),
- }
- }
-}