Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ast): add AstBuilder::vec_convert #7860

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions crates/oxc_ast/src/ast_builder_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,26 @@ impl<'a> AstBuilder<'a> {
Vec::from_array_in(array, self.allocator)
}

/// Convert a [`Vec`] of one type to a [`Vec`] of another type.
///
/// This method is useful where the `Src` is inherited from `Dst` or vice versa, so that you can it
/// without any runtime overhead. For example, you can convert a `Vec<'a, Expression>` to a
/// `Vec<'a, Argument>`
#[inline]
pub fn vec_convert<Src, Dst>(&self, vec: Vec<'a, Src>) -> Vec<'a, Dst>
where
Src: 'a,
Dst: 'a,
{
// Verify types are layout-compatible
debug_assert!(std::mem::size_of::<Src>() == std::mem::size_of::<Dst>());
debug_assert!(std::mem::align_of::<Src>() == std::mem::align_of::<Dst>());

// SAFETY: Vec<Src> and Vec<Dst> have the same layout when Src and Dst have
// the same size and alignment. The lifetimes and allocator remain the same.
unsafe { std::mem::transmute(vec) }
}

/// Move a string slice into the memory arena, returning a reference to the slice
/// in the heap.
#[inline]
Expand Down
Loading