barncastle / BitsKit

A C# library for efficient bit-level reading and writing also adding bit field support
MIT License
34 stars 0 forks source link

Add InlineArray Auto-Gen feature support #3

Open sgf opened 1 month ago

sgf commented 1 month ago

because this feature is often used together with bit-field, so it would be good to put it in the same library.

barncastle commented 1 month ago

Great feature idea! I've not had much exposure to Inline Arrays but this shouldn't be too difficult to implement

In terms of implementation; do you have an example use case for this? I'm wondering if this should support all integral inline arrays or just byte ones

sgf commented 1 month ago

I had a similar open source project. But I abandoned that project because your implementation is better.

https://github.com/sgf/BitX

public struct  MyStruct{
public byte Field1;
public byte Field2;
public short Field3;
}

public struct TestStruct1
{
    public byte BF1;
    public int BF2;
    [InlineLen(3)]
    public MyStruct  buff; //InlineLen is a custome Attribute by your library maybe u can chose a better name
}

//generated maybe like :
[System.Runtime.CompilerServices.InlineArray(3)]
public struct MyStruct_3
{
    private MyStruct _element0;

    public const int Length = 3;

    //not need to define ,because InlineArray has been implemented
    //public Span<MyStruct> GetSpan() => MemoryMarshal.CreateSpan(ref Unsafe.As<MyStruct>(ref this),3);
    //public MyStruct this[int index] {{ get => this[index]; set => this[index] = value; }}

}
sgf commented 1 month ago

This feature is mainly used to allow custom structures to support fixed buffers, but the official implementation is not elegant and practical. Therefore, it is necessary to combine it with a source code generator to make the code writing process simpler.

Of course, the frequency of use of this feature itself is not very high.