Skip to content

Add a Span.TryCast extension method #24740

Description

@ianhays

Split from https://github.com/dotnet/corefx/issues/26368

namespace System
{
    public static partial class MemoryExtensions
    {
        public static bool TryCast<TFrom, TTo>(this ReadOnlySpan<TFrom> source, out ReadOnlySpan<TTo> output) where TFrom : struct where TTo : struct;
        public static bool TryCast<TFrom, TTo>(this Span<TFrom> source, out Span<TTo> output) where TFrom : struct where TTo : struct;
    }
}

We should add some TryCast extension methods that behave similar to the NonPortableCast functions but that include checking beforehand to make sure the operation will be succeed with a usable Span result. To do this, they will need a structure somewhat like this:

if (Platform detect intel)
{
  // do NonPortableCast
  // return true;
}
else
{
 // check the alignment manually. 
 if ( we're already aligned)
 { 
    // do NonPortableCast
    return true;
  }
  else
  {
    //do nothing
    return false;
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-suggestionEarly API idea and discussion, it is NOT ready for implementationarea-System.Memory

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions