Skip to content

Commit f46e244

Browse files
mangelatsLuthaf
authored andcommitted
Add acess methods to the generated SoA slices
1 parent c6e8a42 commit f46e244

File tree

1 file changed

+322
-0
lines changed

1 file changed

+322
-0
lines changed

soa-derive-internal/src/index.rs

Lines changed: 322 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pub fn derive(input: &Input) -> TokenStream {
1414
.collect::<Vec<_>>();
1515
let fields_names_1 = &fields_names;
1616
let fields_names_2 = &fields_names;
17+
let first_field_name = &fields_names[0];
1718

1819
quote!{
1920
// usize
@@ -336,5 +337,326 @@ pub fn derive(input: &Input) -> TokenStream {
336337
(0..=self.end).index_mut(soa)
337338
}
338339
}
340+
341+
// usize
342+
impl<'a> ::soa_derive::SoaIndex<#slice_name<'a>> for usize {
343+
type RefOutput = #ref_name<'a>;
344+
345+
#[inline]
346+
fn get(self, slice: #slice_name<'a>) -> Option<Self::RefOutput> {
347+
if self < slice.#first_field_name.len() {
348+
Some(unsafe { self.get_unchecked(slice) })
349+
} else {
350+
None
351+
}
352+
}
353+
354+
#[inline]
355+
unsafe fn get_unchecked(self, slice: #slice_name<'a>) -> Self::RefOutput {
356+
#ref_name {
357+
#(#fields_names_1: slice.#fields_names_2.get_unchecked(self),)*
358+
}
359+
}
360+
361+
#[inline]
362+
fn index(self, slice: #slice_name<'a>) -> Self::RefOutput {
363+
#ref_name {
364+
#(#fields_names_1: & slice.#fields_names_2[self],)*
365+
}
366+
}
367+
}
368+
369+
impl<'a> ::soa_derive::SoaMutIndex<#slice_mut_name<'a>> for usize {
370+
type MutOutput = #ref_mut_name<'a>;
371+
372+
#[inline]
373+
fn get_mut(self, slice: #slice_mut_name<'a>) -> Option<Self::MutOutput> {
374+
if self < slice.len() {
375+
Some(unsafe { self.get_unchecked_mut(slice) })
376+
} else {
377+
None
378+
}
379+
}
380+
381+
#[inline]
382+
unsafe fn get_unchecked_mut(self, slice: #slice_mut_name<'a>) -> Self::MutOutput {
383+
#ref_mut_name {
384+
#(#fields_names_1: slice.#fields_names_2.get_unchecked_mut(self),)*
385+
}
386+
}
387+
388+
#[inline]
389+
fn index_mut(self, slice: #slice_mut_name<'a>) -> Self::MutOutput {
390+
#ref_mut_name {
391+
#(#fields_names_1: &mut slice.#fields_names_2[self],)*
392+
}
393+
}
394+
}
395+
396+
397+
398+
// Range<usize>
399+
impl<'a> ::soa_derive::SoaIndex<#slice_name<'a>> for ::std::ops::Range<usize> {
400+
type RefOutput = #slice_name<'a>;
401+
402+
#[inline]
403+
fn get(self, slice: #slice_name<'a>) -> Option<Self::RefOutput> {
404+
if self.start <= self.end && self.end <= slice.#first_field_name.len() {
405+
unsafe { Some(self.get_unchecked(slice)) }
406+
} else {
407+
None
408+
}
409+
}
410+
411+
#[inline]
412+
unsafe fn get_unchecked(self, slice: #slice_name<'a>) -> Self::RefOutput {
413+
#slice_name {
414+
#(#fields_names_1: slice.#fields_names_2.get_unchecked(self.clone()),)*
415+
}
416+
}
417+
418+
#[inline]
419+
fn index(self, slice: #slice_name<'a>) -> Self::RefOutput {
420+
#slice_name {
421+
#(#fields_names_1: & slice.#fields_names_2[self.clone()],)*
422+
}
423+
}
424+
}
425+
426+
impl<'a> ::soa_derive::SoaMutIndex<#slice_mut_name<'a>> for ::std::ops::Range<usize> {
427+
type MutOutput = #slice_mut_name<'a>;
428+
429+
#[inline]
430+
fn get_mut(self, slice: #slice_mut_name<'a>) -> Option<Self::MutOutput> {
431+
if self.start <= self.end && self.end <= slice.#first_field_name.len() {
432+
unsafe { Some(self.get_unchecked_mut(slice)) }
433+
} else {
434+
None
435+
}
436+
}
437+
438+
#[inline]
439+
unsafe fn get_unchecked_mut(self, slice: #slice_mut_name<'a>) -> Self::MutOutput {
440+
#slice_mut_name {
441+
#(#fields_names_1: slice.#fields_names_2.get_unchecked_mut(self.clone()),)*
442+
}
443+
}
444+
445+
#[inline]
446+
fn index_mut(self, slice: #slice_mut_name<'a>) -> Self::MutOutput {
447+
#slice_mut_name {
448+
#(#fields_names_1: &mut slice.#fields_names_2[self.clone()],)*
449+
}
450+
}
451+
}
452+
453+
454+
455+
// RangeTo<usize>
456+
impl<'a> ::soa_derive::SoaIndex<#slice_name<'a>> for ::std::ops::RangeTo<usize> {
457+
type RefOutput = #slice_name<'a>;
458+
459+
#[inline]
460+
fn get(self, slice: #slice_name<'a>) -> Option<Self::RefOutput> {
461+
(0..self.end).get(slice)
462+
}
463+
464+
#[inline]
465+
unsafe fn get_unchecked(self, slice: #slice_name<'a>) -> Self::RefOutput {
466+
(0..self.end).get_unchecked(slice)
467+
}
468+
469+
#[inline]
470+
fn index(self, slice: #slice_name<'a>) -> Self::RefOutput {
471+
(0..self.end).index(slice)
472+
}
473+
}
474+
475+
impl<'a> ::soa_derive::SoaMutIndex<#slice_mut_name<'a>> for ::std::ops::RangeTo<usize> {
476+
type MutOutput = #slice_mut_name<'a>;
477+
478+
#[inline]
479+
fn get_mut(self, slice: #slice_mut_name<'a>) -> Option<Self::MutOutput> {
480+
(0..self.end).get_mut(slice)
481+
}
482+
483+
#[inline]
484+
unsafe fn get_unchecked_mut(self, slice: #slice_mut_name<'a>) -> Self::MutOutput {
485+
(0..self.end).get_unchecked_mut(slice)
486+
}
487+
488+
#[inline]
489+
fn index_mut(self, slice: #slice_mut_name<'a>) -> Self::MutOutput {
490+
(0..self.end).index_mut(slice)
491+
}
492+
}
493+
494+
495+
// RangeFrom<usize>
496+
impl<'a> ::soa_derive::SoaIndex<#slice_name<'a>> for ::std::ops::RangeFrom<usize> {
497+
type RefOutput = #slice_name<'a>;
498+
499+
#[inline]
500+
fn get(self, slice: #slice_name<'a>) -> Option<Self::RefOutput> {
501+
(self.start..slice.len()).get(slice)
502+
}
503+
504+
#[inline]
505+
unsafe fn get_unchecked(self, slice: #slice_name<'a>) -> Self::RefOutput {
506+
(self.start..slice.len()).get_unchecked(slice)
507+
}
508+
509+
#[inline]
510+
fn index(self, slice: #slice_name<'a>) -> Self::RefOutput {
511+
(self.start..slice.len()).index(slice)
512+
}
513+
}
514+
515+
impl<'a> ::soa_derive::SoaMutIndex<#slice_mut_name<'a>> for ::std::ops::RangeFrom<usize> {
516+
type MutOutput = #slice_mut_name<'a>;
517+
518+
#[inline]
519+
fn get_mut(self, slice: #slice_mut_name<'a>) -> Option<Self::MutOutput> {
520+
(self.start..slice.len()).get_mut(slice)
521+
}
522+
523+
#[inline]
524+
unsafe fn get_unchecked_mut(self, slice: #slice_mut_name<'a>) -> Self::MutOutput {
525+
(self.start..slice.len()).get_unchecked_mut(slice)
526+
}
527+
528+
#[inline]
529+
fn index_mut(self, slice: #slice_mut_name<'a>) -> Self::MutOutput {
530+
(self.start..slice.len()).index_mut(slice)
531+
}
532+
}
533+
534+
535+
// RangeFull
536+
impl<'a> ::soa_derive::SoaIndex<#slice_name<'a>> for ::std::ops::RangeFull {
537+
type RefOutput = #slice_name<'a>;
538+
539+
#[inline]
540+
fn get(self, slice: #slice_name<'a>) -> Option<Self::RefOutput> {
541+
Some(slice)
542+
}
543+
544+
#[inline]
545+
unsafe fn get_unchecked(self, slice: #slice_name<'a>) -> Self::RefOutput {
546+
slice
547+
}
548+
549+
#[inline]
550+
fn index(self, slice: #slice_name<'a>) -> Self::RefOutput {
551+
slice
552+
}
553+
}
554+
555+
impl<'a> ::soa_derive::SoaMutIndex<#slice_mut_name<'a>> for ::std::ops::RangeFull {
556+
type MutOutput = #slice_mut_name<'a>;
557+
558+
#[inline]
559+
fn get_mut(self, slice: #slice_mut_name<'a>) -> Option<Self::MutOutput> {
560+
Some(slice)
561+
}
562+
563+
#[inline]
564+
unsafe fn get_unchecked_mut(self, slice: #slice_mut_name<'a>) -> Self::MutOutput {
565+
slice
566+
}
567+
568+
#[inline]
569+
fn index_mut(self, slice: #slice_mut_name<'a>) -> Self::MutOutput {
570+
slice
571+
}
572+
}
573+
574+
575+
// RangeInclusive<usize>
576+
impl<'a> ::soa_derive::SoaIndex<#slice_name<'a>> for ::std::ops::RangeInclusive<usize> {
577+
type RefOutput = #slice_name<'a>;
578+
579+
#[inline]
580+
fn get(self, slice: #slice_name<'a>) -> Option<Self::RefOutput> {
581+
if *self.end() == usize::MAX {
582+
None
583+
} else {
584+
(*self.start()..self.end() + 1).get(slice)
585+
}
586+
}
587+
588+
#[inline]
589+
unsafe fn get_unchecked(self, slice: #slice_name<'a>) -> Self::RefOutput {
590+
(*self.start()..self.end() + 1).get_unchecked(slice)
591+
}
592+
593+
#[inline]
594+
fn index(self, slice: #slice_name<'a>) -> Self::RefOutput {
595+
(*self.start()..self.end() + 1).index(slice)
596+
}
597+
}
598+
599+
impl<'a> ::soa_derive::SoaMutIndex<#slice_mut_name<'a>> for ::std::ops::RangeInclusive<usize> {
600+
type MutOutput = #slice_mut_name<'a>;
601+
602+
#[inline]
603+
fn get_mut(self, slice: #slice_mut_name<'a>) -> Option<Self::MutOutput> {
604+
if *self.end() == usize::MAX {
605+
None
606+
} else {
607+
(*self.start()..self.end() + 1).get_mut(slice)
608+
}
609+
}
610+
611+
#[inline]
612+
unsafe fn get_unchecked_mut(self, slice: #slice_mut_name<'a>) -> Self::MutOutput {
613+
(*self.start()..self.end() + 1).get_unchecked_mut(slice)
614+
}
615+
616+
#[inline]
617+
fn index_mut(self, slice: #slice_mut_name<'a>) -> Self::MutOutput {
618+
(*self.start()..self.end() + 1).index_mut(slice)
619+
}
620+
}
621+
622+
623+
// RangeToInclusive<usize>
624+
impl<'a> ::soa_derive::SoaIndex<#slice_name<'a>> for ::std::ops::RangeToInclusive<usize> {
625+
type RefOutput = #slice_name<'a>;
626+
627+
#[inline]
628+
fn get(self, slice: #slice_name<'a>) -> Option<Self::RefOutput> {
629+
(0..=self.end).get(slice)
630+
}
631+
632+
#[inline]
633+
unsafe fn get_unchecked(self, slice: #slice_name<'a>) -> Self::RefOutput {
634+
(0..=self.end).get_unchecked(slice)
635+
}
636+
637+
#[inline]
638+
fn index(self, slice: #slice_name<'a>) -> Self::RefOutput {
639+
(0..=self.end).index(slice)
640+
}
641+
}
642+
643+
impl<'a> ::soa_derive::SoaMutIndex<#slice_mut_name<'a>> for ::std::ops::RangeToInclusive<usize> {
644+
type MutOutput = #slice_mut_name<'a>;
645+
646+
#[inline]
647+
fn get_mut(self, slice: #slice_mut_name<'a>) -> Option<Self::MutOutput> {
648+
(0..=self.end).get_mut(slice)
649+
}
650+
651+
#[inline]
652+
unsafe fn get_unchecked_mut(self, slice: #slice_mut_name<'a>) -> Self::MutOutput {
653+
(0..=self.end).get_unchecked_mut(slice)
654+
}
655+
656+
#[inline]
657+
fn index_mut(self, slice: #slice_mut_name<'a>) -> Self::MutOutput {
658+
(0..=self.end).index_mut(slice)
659+
}
660+
}
339661
}
340662
}

0 commit comments

Comments
 (0)