Skip to content

Lazy Rebuild with non-progressive lambdas #1011

@ev1313

Description

@ev1313

Hi,

currently something like this is not possible:

d = Struct(
"foo" / Int32ul,
"bar" / Rebuild(Int32ul, lambda ctx: ctx.baz),
"baz" / Rebuild(Int32ul, lambda ctx: ctx.foo),

You'd need to create custom Padding + Pointer for this behaviour.

The above can probably made possible by modifying Rebuild, Container and ListContainer:
Rebuild will not evaluate the lambda directly, but instead will set the lambda as value in the Container.

When any value is accessed in the Container (in getattr) which is callable, the lambda gets called with the context. Something like this in getattr:

    def __getattr__(self, name):
        try:
            if name in self.__slots__:
                ret = object.__getattribute__(self, name)
                if callable(ret):
                    return ret(self)
                return ret
            else:
                ret = self[name]
                if callable(ret):
                    return ret(self)
                return ret
        except KeyError:
            raise AttributeError(name)

This would especially be helpful in reconstructing offsets.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions