Skip to content

Commit 7ffc2c1

Browse files
committed
feat: from_slice
1 parent c44a4a5 commit 7ffc2c1

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

src/source_map.rs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -190,17 +190,20 @@ impl StaticSourceMap {
190190
}
191191

192192
pub fn from_json(json: String) -> Result<Self> {
193-
let borrowed_value_cell =
194-
BorrowedValueCell::try_new(json.into_bytes(), |owner| {
195-
// We need a mutable slice from our owned data
196-
// SAFETY: We're creating a mutable reference to the owned data.
197-
// The self_cell ensures this reference is valid for the lifetime of the cell.
198-
#[allow(unsafe_code)]
199-
let bytes: &'static mut [u8] = unsafe {
200-
std::slice::from_raw_parts_mut(owner.as_ptr().cast_mut(), owner.len())
201-
};
202-
simd_json::to_borrowed_value(bytes)
203-
})?;
193+
Self::from_slice(json.into_bytes())
194+
}
195+
196+
pub fn from_slice(slice: Vec<u8>) -> Result<Self> {
197+
let borrowed_value_cell = BorrowedValueCell::try_new(slice, |owner| {
198+
// We need a mutable slice from our owned data
199+
// SAFETY: We're creating a mutable reference to the owned data.
200+
// The self_cell ensures this reference is valid for the lifetime of the cell.
201+
#[allow(unsafe_code)]
202+
let bytes: &'static mut [u8] = unsafe {
203+
std::slice::from_raw_parts_mut(owner.as_ptr().cast_mut(), owner.len())
204+
};
205+
simd_json::to_borrowed_value(bytes)
206+
})?;
204207
Ok(Self::from_borrowed_value_cell(Arc::new(
205208
borrowed_value_cell,
206209
)))
@@ -534,6 +537,12 @@ impl SourceMap {
534537
Self::from_json(json)
535538
}
536539

540+
/// Creates a [SourceMap] from a byte slice containing JSON data.
541+
pub fn from_slice(slice: impl Into<Vec<u8>>) -> Result<SourceMap> {
542+
let s = StaticSourceMap::from_slice(slice.into())?;
543+
Ok(SourceMap(SourceMapCell::Static(s.into())))
544+
}
545+
537546
/// Generate source map to a json string.
538547
pub fn to_json(&self) -> Result<String> {
539548
match &self.0 {

0 commit comments

Comments
 (0)