mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-28 06:14:46 +00:00
Convert over Subs to use the new soa crate
This commit is contained in:
parent
586959780b
commit
2567c8b545
17 changed files with 240 additions and 401 deletions
|
@ -8,39 +8,42 @@ use crate::soa_index::Index;
|
|||
///
|
||||
/// Unlike a Rust slice, this is a u32 offset
|
||||
/// rather than a pointer, and the length is u16.
|
||||
pub struct Slice<T> {
|
||||
pub struct Slice<Array, Elem> {
|
||||
pub start: u32,
|
||||
pub length: u16,
|
||||
_marker: core::marker::PhantomData<T>,
|
||||
_marker: core::marker::PhantomData<(Array, Elem)>,
|
||||
}
|
||||
|
||||
impl<T> fmt::Debug for Slice<T> {
|
||||
impl<T, U> fmt::Debug for Slice<T, U> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(
|
||||
f,
|
||||
"Slice {{ start: {}, length: {} }}",
|
||||
self.start, self.length
|
||||
"Slice<{}, {}> {{ start: {}, length: {} }}",
|
||||
core::any::type_name::<T>(),
|
||||
core::any::type_name::<U>(),
|
||||
self.start,
|
||||
self.length
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// derive of copy and clone does not play well with PhantomData
|
||||
|
||||
impl<T> Copy for Slice<T> {}
|
||||
impl<T, U> Copy for Slice<T, U> {}
|
||||
|
||||
impl<T> Clone for Slice<T> {
|
||||
impl<T, U> Clone for Slice<T, U> {
|
||||
fn clone(&self) -> Self {
|
||||
*self
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Default for Slice<T> {
|
||||
impl<T, U> Default for Slice<T, U> {
|
||||
fn default() -> Self {
|
||||
Self::empty()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Slice<T> {
|
||||
impl<Array, Elem> Slice<Array, Elem> {
|
||||
pub fn empty() -> Self {
|
||||
Self {
|
||||
start: 0,
|
||||
|
@ -49,11 +52,11 @@ impl<T> Slice<T> {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn get_slice<'a>(&self, slice: &'a [T]) -> &'a [T] {
|
||||
pub fn get_slice<'a>(&self, slice: &'a [Elem]) -> &'a [Elem] {
|
||||
&slice[self.indices()]
|
||||
}
|
||||
|
||||
pub fn get_slice_mut<'a>(&self, slice: &'a mut [T]) -> &'a mut [T] {
|
||||
pub fn get_slice_mut<'a>(&self, slice: &'a mut [Elem]) -> &'a mut [Elem] {
|
||||
&mut slice[self.indices()]
|
||||
}
|
||||
|
||||
|
@ -78,7 +81,7 @@ impl<T> Slice<T> {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn extend_new(vec: &mut Vec<T>, it: impl IntoIterator<Item = T>) -> Self {
|
||||
pub fn extend_new(vec: &mut Vec<Elem>, it: impl IntoIterator<Item = Elem>) -> Self {
|
||||
let start = vec.len();
|
||||
|
||||
vec.extend(it);
|
||||
|
@ -89,8 +92,8 @@ impl<T> Slice<T> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<T> IntoIterator for Slice<T> {
|
||||
type Item = Index<T>;
|
||||
impl<Array, Elem> IntoIterator for Slice<Array, Elem> {
|
||||
type Item = Index<Array, Elem>;
|
||||
|
||||
#[allow(clippy::type_complexity)]
|
||||
type IntoIter = Map<core::ops::Range<u32>, fn(u32) -> Self::Item>;
|
||||
|
@ -100,13 +103,13 @@ impl<T> IntoIterator for Slice<T> {
|
|||
}
|
||||
}
|
||||
|
||||
fn u32_to_index<T>(i: u32) -> Index<T> {
|
||||
fn u32_to_index<T, U>(i: u32) -> Index<T, U> {
|
||||
Index {
|
||||
index: i,
|
||||
_marker: core::marker::PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
pub trait GetSlice<T> {
|
||||
fn get_slice(&self, slice: Slice<T>) -> &[T];
|
||||
pub trait GetSlice<Array, Elem> {
|
||||
fn get_slice(&self, slice: Slice<Array, Elem>) -> &[Elem];
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue