Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(serialized): deserialize with bytechecked #7460

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 19 additions & 14 deletions crates/swc_common/src/plugin/serialized.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
use std::any::type_name;

use anyhow::Error;
#[cfg(feature = "__rkyv")]
use rkyv::Deserialize;

#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
Expand Down Expand Up @@ -99,18 +95,27 @@ impl PluginSerializedBytes {
}

#[tracing::instrument(level = "info", skip_all)]
pub fn deserialize<W>(&self) -> Result<VersionedSerializable<W>, Error>
pub fn deserialize<'a, W>(&'a self) -> Result<VersionedSerializable<W>, Error>
where
W: rkyv::Archive,
W::Archived: rkyv::Deserialize<W, rkyv::de::deserializers::SharedDeserializeMap>,
W: rkyv::Archive + 'a,
W::Archived: 'a
+ rkyv::CheckBytes<rkyv::validation::validators::DefaultValidator<'a>>
+ rkyv::Deserialize<W, rkyv::de::deserializers::SharedDeserializeMap>,
{
use anyhow::Context;

let archived = unsafe { rkyv::archived_root::<VersionedSerializable<W>>(&self.field[..]) };

archived
.deserialize(&mut rkyv::de::deserializers::SharedDeserializeMap::new())
.with_context(|| format!("failed to deserialize `{}`", type_name::<W>()))
use rkyv::validation::{validators::CheckDeserializeError, CheckArchiveError};

let result = rkyv::from_bytes::<VersionedSerializable<W>>(&self.field[..]);
result.map_err(move |err| match err {
CheckDeserializeError::DeserializeError(e) => e.into(),
CheckDeserializeError::CheckBytesError(e) => match e {
CheckArchiveError::CheckBytesError(_e) => {
// [TODO]: we can't forward CheckBytes::Error itself as it is Archived type of VersionedSerializable struct itself.
// Still we need to carry better diagnostics if this occurs.
Error::msg("CheckBytesError")
}
CheckArchiveError::ContextError(e) => e.into(),
},
})
}
}

Expand Down