⚠ This page is served via a proxy. Original site: https://github.com
This service does not collect credentials or authentication data.
Skip to content

[API Proposal]: Add EndiannessAwarePair<TUpper, TLower> #123073

@LEI-Hongfaan

Description

@LEI-Hongfaan

Background and motivation

Third-party libraries that need endianness-aware layout for a pair of fields must either:

  • compile and distribute separate little-endian and big-endian builds, or
  • implement complex conditional logic, ref T shims, or manual byte-swapping.

This increases maintenance, package size, and the chance of subtle bugs and performance regressions. A small BCL primitive that encodes endianness-aware semantics would let library authors write one build that behaves correctly and efficiently on all platforms.

API Proposal

namespace System.Runtime.InteropServices {

    [StructLayout(LayoutKind.Sequential)]
    public struct EndiannessAwarePair<TUpper, TLower>
        where TUpper : unmanaged
        where TLower : unmanaged {

#if BIGENDIAN
        public TUpper Upper;
        public TLower Lower;
#else
        public TLower Lower;
        public TUpper Upper;
#endif

        public static EndianAwarePair<TLower, TUpper> FromUpperAndLower(TUpper upper, TUpper lower);
        public static EndianAwarePair<TLower, TUpper> FromLowerAndUpper(TLower lower, TLower upper);
    }
}

API Usage

public readonly struct MyInt128 {
    readonly EndiannessAwarePair<Int64, UInt64> bits;
}

Alternative Designs

No response

Risks

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions