Giter Club home page Giter Club logo

Comments (3)

wmorgue avatar wmorgue commented on August 23, 2024

What exactly needs to be implemented ?

from gdnative.

nical avatar nical commented on August 23, 2024

Something similar to the mut and non-mut iterators that std::Vec provide would be great.
Right now you can't do for element in &variant_array { /* ... */ } and it should be easy to add.

from gdnative.

ctbur avatar ctbur commented on August 23, 2024

I've taken a look at this. For Godot arrays with copy types, we cannot implement for element in &mut prim_array, because for that we need a pointer to the element, which we cannot obtain afaik. So what's left is element in &prim_array and element in prim_array. For these we can implement this macro:

macro_rules! impl_iter {
    ($array_ty:ty, $element_ty:ty, $iter:ident) => (
        pub struct $iter<'a> {
            array: &'a $array_ty,
            index: i32,
            len: i32,
        }

        impl<'a> Iterator for $iter<'a> {
            type Item = $element_ty;

            fn next(&mut self) -> Option<Self::Item> {
                if self.index < self.len {
                    let ret = Some(self.array.get(self.index));
                    self.index += 1;
                    return ret;
                } else {
                    return None;
                }
            }

            fn size_hint(&self) -> (usize, Option<usize>) {
                (self.len as usize, Some(self.len as usize))
            }
        }

        impl<'a> IntoIterator for &'a $array_ty {
            type Item = $element_ty;
            type IntoIter = $iter<'a>;

            fn into_iter(self) -> $iter<'a> {
                let len = self.len();

                $iter {
                    array: self,
                    index: 0,
                    len
                }
            }
        }
    )
}

And apply it with, e.g.: impl_iter!(ByteArray, u8, ByteIter);.

If we, however, had a trait GodotArray<T> which implements len() -> usize, get(i32) -> T and possibly set(i32, T) for all arrays, we should be able to get away with just generics instead of a macro, by implementing IntoIterator for all T: GodotArray<T>. For that I would have to make larger modifications. But at this point maybe the Godot array types could be unified even further, because they share most of their structure.

from gdnative.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.