Skip to content

Commit d35ec0d

Browse files
committed
feat: from_slice
1 parent c44a4a5 commit d35ec0d

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/source_map.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,12 @@ impl StaticSourceMap {
190190
}
191191

192192
pub fn from_json(json: String) -> Result<Self> {
193+
Self::from_slice(json.into_bytes())
194+
}
195+
196+
pub fn from_slice(slice: Vec<u8>) -> Result<Self> {
193197
let borrowed_value_cell =
194-
BorrowedValueCell::try_new(json.into_bytes(), |owner| {
198+
BorrowedValueCell::try_new(slice, |owner| {
195199
// We need a mutable slice from our owned data
196200
// SAFETY: We're creating a mutable reference to the owned data.
197201
// The self_cell ensures this reference is valid for the lifetime of the cell.
@@ -534,6 +538,12 @@ impl SourceMap {
534538
Self::from_json(json)
535539
}
536540

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

0 commit comments

Comments
 (0)