From c9de0146ea191f83da34fbe6ec014aad4c6da14f Mon Sep 17 00:00:00 2001 From: OJ Kwon Date: Wed, 29 Jun 2022 15:18:00 -0700 Subject: [PATCH 1/2] feat(swc/common): interfaces for versioned wrapper --- crates/swc_common/src/plugin.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/crates/swc_common/src/plugin.rs b/crates/swc_common/src/plugin.rs index afc77ad33fb6..08d7e7baac93 100644 --- a/crates/swc_common/src/plugin.rs +++ b/crates/swc_common/src/plugin.rs @@ -174,4 +174,20 @@ impl Serialized { #[derive(Archive, Deserialize, Serialize)] #[repr(transparent)] #[archive_attr(repr(transparent), derive(CheckBytes))] -pub struct VersionedSerializable(#[with(AsBox)] pub (u32, T)); +pub struct VersionedSerializable(#[with(AsBox)] (u32, T)); + +impl VersionedSerializable { + pub fn new(value: T) -> Self { + // TODO: we'll add compile time flag to augment schema version per binary. + // 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 + } +} From 72a3930320ee2f5f034be333a9661bfa445b7034 Mon Sep 17 00:00:00 2001 From: OJ Kwon Date: Wed, 29 Jun 2022 16:23:15 -0700 Subject: [PATCH 2/2] feat(swc/common): allow to take inner from versioned --- crates/swc_common/src/plugin.rs | 11 +++++++++-- crates/swc_ecma_ast/src/module.rs | 6 ++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/crates/swc_common/src/plugin.rs b/crates/swc_common/src/plugin.rs index 08d7e7baac93..ff55d2c706ea 100644 --- a/crates/swc_common/src/plugin.rs +++ b/crates/swc_common/src/plugin.rs @@ -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; @@ -178,7 +178,7 @@ pub struct VersionedSerializable(#[with(AsBox)] (u32, T)); impl VersionedSerializable { pub fn new(value: T) -> Self { - // TODO: we'll add compile time flag to augment schema version per binary. + // TODO: we'll add compile time flag to augment schema version. // User should not try to set version by themselves. VersionedSerializable((1, value)) } @@ -190,4 +190,11 @@ impl VersionedSerializable { pub fn inner(&self) -> &T { &self.0 .1 } + + pub fn take(&mut self) -> T + where + T: Default, + { + mem::take(&mut self.0 .1) + } } diff --git a/crates/swc_ecma_ast/src/module.rs b/crates/swc_ecma_ast/src/module.rs index 4bc570f110a4..838a33bf3350 100644 --- a/crates/swc_ecma_ast/src/module.rs +++ b/crates/swc_ecma_ast/src/module.rs @@ -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 {