Skip to content

Commit 89966ab

Browse files
committed
refactor(swc/common): use into_inner instead of take
1 parent 42df149 commit 89966ab

File tree

13 files changed

+28
-91
lines changed

13 files changed

+28
-91
lines changed

crates/swc/src/plugin.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ impl RustPlugins {
164164

165165
// Plugin transformation is done. Deserialize transformed bytes back
166166
// into Program
167-
serialized.deserialize().map(|mut v| v.take())
167+
serialized.deserialize().map(|v| v.into_inner())
168168
},
169169
)
170170
}
@@ -221,7 +221,7 @@ impl RustPlugins {
221221
}
222222
}
223223

224-
serialized.deserialize().map(|mut v| v.take())
224+
serialized.deserialize().map(|v| v.into_inner())
225225
},
226226
)
227227
}

crates/swc_common/src/comments.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -548,16 +548,6 @@ pub struct Comment {
548548
pub text: String,
549549
}
550550

551-
impl Default for Comment {
552-
fn default() -> Self {
553-
Comment {
554-
kind: CommentKind::Line,
555-
span: DUMMY_SP,
556-
text: Default::default(),
557-
}
558-
}
559-
}
560-
561551
impl Spanned for Comment {
562552
fn span(&self) -> Span {
563553
self.span

crates/swc_common/src/errors/diagnostic.rs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,6 @@ pub struct Diagnostic {
3636
pub suggestions: Vec<CodeSuggestion>,
3737
}
3838

39-
impl Default for Diagnostic {
40-
fn default() -> Self {
41-
Diagnostic {
42-
level: Level::Bug,
43-
message: vec![],
44-
code: None,
45-
span: MultiSpan::new(),
46-
children: vec![],
47-
suggestions: vec![],
48-
}
49-
}
50-
}
51-
5239
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
5340
#[cfg_attr(
5441
feature = "diagnostic-serde",

crates/swc_common/src/plugin.rs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,6 @@ pub enum PluginError {
4141
Serialize(String),
4242
}
4343

44-
impl Default for PluginError {
45-
fn default() -> Self {
46-
PluginError::Deserialize("Default serialization error".to_string())
47-
}
48-
}
49-
5044
/// Wraps internal representation of serialized data for exchanging data between
5145
/// plugin to the host. Consumers should not rely on specific details of byte
5246
/// format struct contains: it is strict implementation detail which can
@@ -200,10 +194,7 @@ impl<T> VersionedSerializable<T> {
200194
&self.0 .1
201195
}
202196

203-
pub fn take(&mut self) -> T
204-
where
205-
T: Default,
206-
{
207-
mem::take(&mut self.0 .1)
197+
pub fn into_inner(self) -> T {
198+
self.0 .1
208199
}
209200
}

crates/swc_common/src/syntax_pos.rs

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,6 @@ pub enum FileName {
130130
Custom(String),
131131
}
132132

133-
impl Default for FileName {
134-
fn default() -> Self {
135-
FileName::Anon
136-
}
137-
}
138-
139133
/// A wrapper that attempts to convert a type to and from UTF-8.
140134
///
141135
/// Types like `OsString` and `PathBuf` aren't guaranteed to be encoded as
@@ -811,7 +805,7 @@ impl Sub<BytePos> for NonNarrowChar {
811805
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
812806
)]
813807
#[cfg_attr(feature = "rkyv", archive_attr(repr(C), derive(bytecheck::CheckBytes)))]
814-
#[derive(Clone, Default)]
808+
#[derive(Clone)]
815809
pub struct SourceFile {
816810
/// The name of the file that the source came from. Source that doesn't
817811
/// originate from files has names between angle brackets by convention,
@@ -1004,9 +998,7 @@ pub trait Pos {
1004998
/// - Values larger than `u32::MAX - 2^16` are reserved for the comments.
1005999
///
10061000
/// `u32::MAX` is special value used to generate source map entries.
1007-
#[derive(
1008-
Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Debug, Serialize, Deserialize, Default,
1009-
)]
1001+
#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Debug, Serialize, Deserialize)]
10101002
#[serde(transparent)]
10111003
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
10121004
#[cfg_attr(
@@ -1040,7 +1032,7 @@ impl BytePos {
10401032
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
10411033
)]
10421034
#[cfg_attr(feature = "rkyv", archive_attr(repr(C), derive(bytecheck::CheckBytes)))]
1043-
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug, Default)]
1035+
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
10441036
pub struct CharPos(pub usize);
10451037

10461038
// FIXME: Lots of boilerplate in these impls, but so far my attempts to fix
@@ -1136,7 +1128,7 @@ impl Sub for CharPos {
11361128
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
11371129
)]
11381130
#[cfg_attr(feature = "rkyv", archive_attr(repr(C), derive(bytecheck::CheckBytes)))]
1139-
#[derive(Debug, Clone, Default)]
1131+
#[derive(Debug, Clone)]
11401132
pub struct Loc {
11411133
/// Information about the original source
11421134
pub file: Lrc<SourceFile>,
@@ -1177,15 +1169,6 @@ pub struct SourceFileAndBytePos {
11771169
pub pos: BytePos,
11781170
}
11791171

1180-
impl Default for SourceFileAndBytePos {
1181-
fn default() -> Self {
1182-
SourceFileAndBytePos {
1183-
sf: Lrc::new(SourceFile::default()),
1184-
pos: Default::default(),
1185-
}
1186-
}
1187-
}
1188-
11891172
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
11901173
#[cfg_attr(
11911174
feature = "rkyv",
@@ -1210,7 +1193,6 @@ pub struct LineCol {
12101193
pub col: u32,
12111194
}
12121195

1213-
#[derive(Default)]
12141196
#[cfg_attr(
12151197
feature = "rkyv",
12161198
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
@@ -1242,12 +1224,6 @@ pub enum SpanLinesError {
12421224
DistinctSources(DistinctSources),
12431225
}
12441226

1245-
impl Default for SpanLinesError {
1246-
fn default() -> Self {
1247-
SpanLinesError::IllFormedSpan(DUMMY_SP)
1248-
}
1249-
}
1250-
12511227
#[derive(Clone, PartialEq, Eq, Debug)]
12521228
pub enum SpanSnippetError {
12531229
DummyBytePos,

crates/swc_common/src/syntax_pos/hygiene.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ struct MarkData {
6767
is_builtin: bool,
6868
}
6969

70-
#[derive(Default)]
7170
#[cfg_attr(
7271
feature = "rkyv",
7372
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
@@ -199,7 +198,7 @@ impl Mark {
199198
len.try_into().expect("Should able to convert ptr length"),
200199
)
201200
.expect("Should able to deserialize")
202-
.take()
201+
.into_inner()
203202
};
204203
self = Mark::from_u32(context.0);
205204

@@ -238,7 +237,7 @@ impl Mark {
238237
len.try_into().expect("Should able to convert ptr length"),
239238
)
240239
.expect("Should able to deserialize")
241-
.take()
240+
.into_inner()
242241
};
243242
a = Mark::from_u32(context.0);
244243
b = Mark::from_u32(context.1);
@@ -403,7 +402,7 @@ impl SyntaxContext {
403402
len.try_into().expect("Should able to convert ptr length"),
404403
)
405404
.expect("Should able to deserialize")
406-
.take()
405+
.into_inner()
407406
};
408407

409408
*self = SyntaxContext(context.0);

crates/swc_ecma_ast/src/module.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,6 @@ 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-
2317
#[ast_node("Module")]
2418
#[derive(Eq, Hash, EqIgnoreSpan)]
2519
pub struct Module {

crates/swc_plugin_macro/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,14 @@ fn handle_func(func: ItemFn) -> TokenStream {
6262
pub fn #process_impl_ident(ast_ptr: *const u8, ast_ptr_len: i32, config_str_ptr: *const u8, config_str_ptr_len: i32, context_str_ptr: *const u8, context_str_ptr_len: i32, should_enable_comments_proxy: i32) -> i32 {
6363
// Reconstruct `Program` & config string from serialized program
6464
// Host (SWC) should allocate memory, copy bytes and pass ptr to plugin.
65-
let program = unsafe { swc_plugin::deserialize_from_ptr(ast_ptr, ast_ptr_len).map(|mut v| v.take()) };
65+
let program = unsafe { swc_plugin::deserialize_from_ptr(ast_ptr, ast_ptr_len).map(|v| v.into_inner()) };
6666
if program.is_err() {
6767
let err = swc_plugin::PluginError::Deserialize("Failed to deserialize program received from host".to_string());
6868
return construct_error_ptr(err);
6969
}
7070
let program: Program = program.expect("Should be a program");
7171

72-
let config = unsafe { swc_plugin::deserialize_from_ptr(config_str_ptr, config_str_ptr_len).map(|mut v| v.take()) };
72+
let config = unsafe { swc_plugin::deserialize_from_ptr(config_str_ptr, config_str_ptr_len).map(|v| v.into_inner()) };
7373
if config.is_err() {
7474
let err = swc_plugin::PluginError::Deserialize(
7575
"Failed to deserialize config string received from host".to_string()
@@ -78,7 +78,7 @@ fn handle_func(func: ItemFn) -> TokenStream {
7878
}
7979
let config: String = config.expect("Should be a string");
8080

81-
let context = unsafe { swc_plugin::deserialize_from_ptr(context_str_ptr, context_str_ptr_len).map(|mut v| v.take()) };
81+
let context = unsafe { swc_plugin::deserialize_from_ptr(context_str_ptr, context_str_ptr_len).map(|v| v.into_inner()) };
8282
if context.is_err() {
8383
let err = swc_plugin::PluginError::Deserialize("Failed to deserialize context string received from host".to_string());
8484
return construct_error_ptr(err);

crates/swc_plugin_proxy/src/memory_interop/read_returned_result_from_host.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use swc_common::plugin::{
55
};
66

77
/// A struct to exchange allocated data between memory spaces.
8-
#[derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Default)]
8+
#[derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
99
#[archive_attr(repr(C), derive(bytecheck::CheckBytes))]
1010
pub struct AllocatedBytesPtr(pub i32, pub i32);
1111

@@ -62,7 +62,7 @@ where
6262
pub fn read_returned_result_from_host<F, R>(f: F) -> Option<R>
6363
where
6464
F: FnOnce(i32) -> i32,
65-
R: rkyv::Archive + std::default::Default,
65+
R: rkyv::Archive,
6666
R::Archived: rkyv::Deserialize<R, rkyv::de::deserializers::SharedDeserializeMap>,
6767
{
6868
let allocated_returned_value_ptr = read_returned_result_from_host_inner(f);
@@ -74,7 +74,7 @@ where
7474
allocated_returned_value_ptr.1,
7575
)
7676
.expect("Returned value should be serializable")
77-
.take()
77+
.into_inner()
7878
})
7979
}
8080

@@ -88,7 +88,7 @@ where
8888
pub fn read_returned_result_from_host_fallible<F, R>(f: F) -> Option<R>
8989
where
9090
F: FnOnce(i32) -> i32,
91-
R: rkyv::Archive + std::default::Default,
91+
R: rkyv::Archive,
9292
R::Archived: rkyv::Deserialize<R, rkyv::de::deserializers::SharedDeserializeMap>,
9393
{
9494
// Allocate AllocatedBytesPtr to get return value from the host
@@ -118,7 +118,7 @@ where
118118
serialized_allocated_bytes_raw_ptr_size as i32,
119119
)
120120
.expect("Should able to deserialize AllocatedBytesPtr")
121-
.take()
121+
.into_inner()
122122
};
123123

124124
// Using AllocatedBytesPtr's value, reconstruct actual return value
@@ -128,6 +128,6 @@ where
128128
allocated_returned_value_ptr.1,
129129
)
130130
.expect("Returned value should be serializable")
131-
.take()
131+
.into_inner()
132132
})
133133
}

crates/swc_plugin_runner/src/imported_fn/comments.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ pub fn add_leading_comment_proxy(env: &CommentHostEnvironment, byte_pos: u32) {
125125
serialized
126126
.deserialize()
127127
.expect("Should be able to deserialize")
128-
.take(),
128+
.into_inner(),
129129
);
130130
});
131131
}
@@ -137,7 +137,7 @@ pub fn add_leading_comments_proxy(env: &CommentHostEnvironment, byte_pos: u32) {
137137
serialized
138138
.deserialize()
139139
.expect("Should be able to deserialize")
140-
.take(),
140+
.into_inner(),
141141
);
142142
});
143143
}
@@ -224,7 +224,7 @@ pub fn add_trailing_comment_proxy(env: &CommentHostEnvironment, byte_pos: u32) {
224224
serialized
225225
.deserialize()
226226
.expect("Should be able to deserialize")
227-
.take(),
227+
.into_inner(),
228228
);
229229
});
230230
}
@@ -236,7 +236,7 @@ pub fn add_trailing_comments_proxy(env: &CommentHostEnvironment, byte_pos: u32)
236236
serialized
237237
.deserialize()
238238
.expect("Should be able to deserialize")
239-
.take(),
239+
.into_inner(),
240240
);
241241
});
242242
}

crates/swc_plugin_runner/src/imported_fn/handler.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub fn emit_diagnostics(env: &BaseHostEnvironment, bytes_ptr: i32, bytes_ptr_len
1313
let serialized = PluginSerializedBytes::from_slice(&diagnostics_bytes[..]);
1414
let diagnostic = PluginSerializedBytes::deserialize::<Diagnostic>(&serialized)
1515
.expect("Should able to be deserialized into diagnostic")
16-
.take();
16+
.into_inner();
1717

1818
let mut builder =
1919
swc_common::errors::DiagnosticBuilder::new_diagnostic(handler, diagnostic);

crates/swc_plugin_runner/src/transform_executor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ impl TransformExecutor {
8686
if returned_ptr_result == 0 {
8787
Ok(ret)
8888
} else {
89-
let err: PluginError = ret.deserialize()?.take();
89+
let err: PluginError = ret.deserialize()?.into_inner()();
9090
match err {
9191
PluginError::SizeInteropFailure(msg) => Err(anyhow!(
9292
"Failed to convert pointer size to calculate: {}",

crates/swc_plugin_runner/tests/integration.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ fn internal() -> Result<(), Error> {
112112
let program: Program = program_bytes
113113
.deserialize()
114114
.expect("Should able to deserialize")
115-
.take();
115+
.into_inner();
116116
let mut visitor = TestVisitor {
117117
plugin_transform_found: false,
118118
};
@@ -226,7 +226,7 @@ fn internal() -> Result<(), Error> {
226226
let program: Program = serialized_program
227227
.deserialize()
228228
.expect("Should able to deserialize")
229-
.take();
229+
.into_inner();
230230
let mut visitor = TestVisitor {
231231
plugin_transform_found: false,
232232
};

0 commit comments

Comments
 (0)