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

Add access methods #27

Closed
mangelats opened this issue Sep 24, 2020 · 3 comments
Closed

Add access methods #27

mangelats opened this issue Sep 24, 2020 · 3 comments

Comments

@mangelats
Copy link
Contributor

mangelats commented Sep 24, 2020

Add the methods get, get_mut, get_unchecked, and get_unchecked_mut of Vec to the generated SoA.

Right now it's not possible implement the trait Index and IndexMut. This issue would add a way to access the elements in a way that also works for Vec. This would also make accessing the underlying Vec not required.

// Before
let value = vec.field[1];

// After
let value = vec.get(1)?.field; // vec could be an ExampleVec or a Vec<Example>

And we might get a reference (which we couldn't do before):

let reference = vec.get(2)?;

Also would support ranges:

let slice = vec.get(0..5)?;

And we should probably add ranges to our custom slice so it's possible to get an smaller slice from a slice.

To do so, it would make sense to make a trait similar to SliceIndex but changing the output to not require returning a reference (so we can use our struct of references).

@mangelats
Copy link
Contributor Author

Really useful for the issue #19 where you don't have underlying vectors, so this gives a common interface for both versions and Vec.

@Luthaf
Copy link
Member

Luthaf commented Sep 24, 2020

Sounds good to me!

@mangelats
Copy link
Contributor Author

mangelats commented Sep 24, 2020

Also adding index and index_mut because get actually returns an Option<T>. It will keep the same behaviour as using the index operator:

// vec: Vec<Example>
vec[0];

// soa: ExampleVec
soa.index(0);

Should behave the same (even when it fails) and work with ranges :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants