Skip to content

Commit e1aa937

Browse files
authored
feat(plugin): Allow taking the inner data from versioned (#5071)
1 parent dcc4f2a commit e1aa937

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

crates/swc_common/src/plugin.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//! `swc_common`.
55
#![allow(unused)]
66

7-
use std::any::type_name;
7+
use std::{any::type_name, mem};
88

99
use anyhow::Error;
1010
use bytecheck::CheckBytes;
@@ -174,4 +174,27 @@ impl Serialized {
174174
#[derive(Archive, Deserialize, Serialize)]
175175
#[repr(transparent)]
176176
#[archive_attr(repr(transparent), derive(CheckBytes))]
177-
pub struct VersionedSerializable<T>(#[with(AsBox)] pub (u32, T));
177+
pub struct VersionedSerializable<T>(#[with(AsBox)] (u32, T));
178+
179+
impl<T> VersionedSerializable<T> {
180+
pub fn new(value: T) -> Self {
181+
// TODO: we'll add compile time flag to augment schema version.
182+
// User should not try to set version by themselves.
183+
VersionedSerializable((1, value))
184+
}
185+
186+
pub fn version(&self) -> u32 {
187+
self.0 .0
188+
}
189+
190+
pub fn inner(&self) -> &T {
191+
&self.0 .1
192+
}
193+
194+
pub fn take(&mut self) -> T
195+
where
196+
T: Default,
197+
{
198+
mem::take(&mut self.0 .1)
199+
}
200+
}

crates/swc_ecma_ast/src/module.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ pub enum Program {
1414
Script(Script),
1515
}
1616

17+
impl Default for Program {
18+
fn default() -> Self {
19+
Program::Module(Module::dummy())
20+
}
21+
}
22+
1723
#[ast_node("Module")]
1824
#[derive(Eq, Hash, EqIgnoreSpan)]
1925
pub struct Module {

0 commit comments

Comments
 (0)