feat(plugin): Allow taking the inner data from versioned (#5071)

This commit is contained in:
OJ Kwon 2022-06-29 18:21:12 -07:00 committed by GitHub
parent dcc4f2a706
commit e1aa937183
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 2 deletions

View File

@ -4,7 +4,7 @@
//! `swc_common`.
#![allow(unused)]
use std::any::type_name;
use std::{any::type_name, mem};
use anyhow::Error;
use bytecheck::CheckBytes;
@ -174,4 +174,27 @@ impl Serialized {
#[derive(Archive, Deserialize, Serialize)]
#[repr(transparent)]
#[archive_attr(repr(transparent), derive(CheckBytes))]
pub struct VersionedSerializable<T>(#[with(AsBox)] pub (u32, T));
pub struct VersionedSerializable<T>(#[with(AsBox)] (u32, T));
impl<T> VersionedSerializable<T> {
pub fn new(value: T) -> Self {
// TODO: we'll add compile time flag to augment schema version.
// User should not try to set version by themselves.
VersionedSerializable((1, value))
}
pub fn version(&self) -> u32 {
self.0 .0
}
pub fn inner(&self) -> &T {
&self.0 .1
}
pub fn take(&mut self) -> T
where
T: Default,
{
mem::take(&mut self.0 .1)
}
}

View File

@ -14,6 +14,12 @@ pub enum Program {
Script(Script),
}
impl Default for Program {
fn default() -> Self {
Program::Module(Module::dummy())
}
}
#[ast_node("Module")]
#[derive(Eq, Hash, EqIgnoreSpan)]
pub struct Module {