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

Vector's iter method is private for non public structs #69

Open
akhilman opened this issue Nov 1, 2024 · 5 comments
Open

Vector's iter method is private for non public structs #69

akhilman opened this issue Nov 1, 2024 · 5 comments

Comments

@akhilman
Copy link

akhilman commented Nov 1, 2024

I have an error:

error[E0624]: method `iter` is private
  --> src/main.rs:12:10
   |
3  | #[derive(Debug, StructOfArray)]
   |                 ------------- private method defined here
...
12 |     mega.iter().for_each(|row| {
   |          ^^^^ private method

In this code:

use soa_derive::StructOfArray;

#[derive(Debug, StructOfArray)]
#[soa_derive(Debug)]
struct MegaStruct {
    foo: usize,
    bar: usize,
}

fn main() {
    let mega = MegaStructVec::default();
    mega.iter().for_each(|row| {
        println!("{:?}", row);
    });
}

What I am doing wrong?

@akhilman
Copy link
Author

akhilman commented Nov 1, 2024

Ok. found a cause. The struct should be public to make iter accessible.

@akhilman akhilman changed the title Vector's iter method is private Vector's iter method is private for non public structs Nov 1, 2024
@akhilman
Copy link
Author

akhilman commented Nov 2, 2024

So we have the __detail_iter_basestructname submodule where the Iter and the IterMut structures are defined. The definition of these iterators will not escape the submodule if the original structure is not public.

Could we have BaseStructNameIter and BaseStructNameIterMut without the submodule instead?

@Luthaf
Copy link
Member

Luthaf commented Nov 4, 2024

Is the issue coming from the fact that the function is private or that the returned types are private?

We could make the types public anyway, I think I remember that creating "detail" modules like this was a bit problematic for the compiler.

@akhilman
Copy link
Author

akhilman commented Nov 4, 2024

All methods defined for …Vector and …Slice inside the __detail_iter_… module are generated with the same visibility as the original structure. So they are not visible outside tihs module if the original structure is not public.

#[allow(non_snake_case, dead_code)]
mod __detail_iter_megastruct {
    ...
    impl MegaStructVec {
        /// Get a mutable iterator over the
        ///[`MegaStructRefMut`](struct.MegaStructRefMut.html)
        /// in this vector
        fn iter_mut(&mut self) -> IterMut {
            self.as_mut_slice().into_iter()
        }
    }
    impl<'a> MegaStructSliceMut<'a> {
        /// Get an iterator over the
        ///[`MegaStructRef`](struct.MegaStructRef.html)
        /// in this vector
        fn iter(&mut self) -> Iter {
            self.as_ref().into_iter()
        }
        /// Get a mutable iterator over the
        ///[`MegaStructRefMut`](struct.MegaStructRefMut.html)
        /// in this vector
        fn iter_mut(&mut self) -> IterMut {
            IterMut(self.foo.iter_mut().zip(self.bar.iter_mut()))
        }
        /// Get a mutable iterator over the
        ///[`MegaStructRefMut`](struct.MegaStructRefMut.html)
        /// in this vector
        fn into_iter(self) -> IterMut<'a> {
            IterMut(self.foo.iter_mut().zip(self.bar.iter_mut()))
        }
    }
    ...
}

@akhilman
Copy link
Author

akhilman commented Nov 4, 2024

If we define the original structure as pub(super), so that it's visibility becomes relative to the module where it is defined, then Iter, IterMut and all associated functions of …Vec and …Slice became pub(super), but inside the __detail_iter_… module, so they are not accessible from the parent module as original structure.

pub(super) struct MegaStruct {
    foo: usize,
    bar: usize,
}#[allow(dead_code)]
pub(super) struct MegaStructVec {}#[allow(non_snake_case, dead_code)]
mod __detail_iter_megastruct {
    use super::*;
    use std::slice;
    #[allow(unused_imports)]
    use std::iter;
    #[allow(missing_debug_implementations)]
    pub(super) struct Iter<'a>(
        pub(super) iter::Zip<slice::Iter<'a, usize>, slice::Iter<'a, usize>>,
    );impl MegaStructVec {
        /// Get an iterator over the
        ///[`MegaStructRef`](struct.MegaStructRef.html)
        /// in this vector
        pub(super) fn iter(&self) -> Iter {
            self.as_slice().into_iter()
        }
    }
    impl<'a> MegaStructSlice<'a> {
        /// Get an iterator over the
        ///[`MegaStructRef`](struct.MegaStructRef.html)
        /// in this slice.
        pub(super) fn iter(&self) -> Iter {
            Iter(self.foo.iter().zip(self.bar.iter()))
        }
        /// Get an iterator over the
        ///[`MegaStructRef`](struct.MegaStructRef.html)
        /// in this slice.
        pub(super) fn into_iter(self) -> Iter<'a> {
            Iter(self.foo.iter().zip(self.bar.iter()))
        }
    }
    #[allow(missing_debug_implementations)]
    pub(super) struct IterMut<'a>(
        pub(super) iter::Zip<slice::IterMut<'a, usize>, slice::IterMut<'a, usize>>,
    );impl MegaStructVec {
        /// Get a mutable iterator over the
        ///[`MegaStructRefMut`](struct.MegaStructRefMut.html)
        /// in this vector
        pub(super) fn iter_mut(&mut self) -> IterMut {
            self.as_mut_slice().into_iter()
        }
    }
    impl<'a> MegaStructSliceMut<'a> {
        /// Get an iterator over the
        ///[`MegaStructRef`](struct.MegaStructRef.html)
        /// in this vector
        pub(super) fn iter(&mut self) -> Iter {
            self.as_ref().into_iter()
        }
        /// Get a mutable iterator over the
        ///[`MegaStructRefMut`](struct.MegaStructRefMut.html)
        /// in this vector
        pub(super) fn iter_mut(&mut self) -> IterMut {
            IterMut(self.foo.iter_mut().zip(self.bar.iter_mut()))
        }
        /// Get a mutable iterator over the
        ///[`MegaStructRefMut`](struct.MegaStructRefMut.html)
        /// in this vector
        pub(super) fn into_iter(self) -> IterMut<'a> {
            IterMut(self.foo.iter_mut().zip(self.bar.iter_mut()))
        }
    }
    impl<'a> soa_derive::SoAIter<'a> for MegaStruct {
        type Iter = Iter<'a>;
        type IterMut = IterMut<'a>;
    }
}

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