CommunityToolkit / CommunityToolkit/dotnet

Span2D Constructor overload

Open
#929 1 comment 0 reactions 0 assignees View on GitHub
feature request :mailbox_with_mail:
Dominant language
C#
Stars
3.8k
Forks
400
PR merge metrics
No merged PRs in 30d

Description

### Overview

A constructor for the `Span2D` to create a 2D view over a `Span`. Currently only arrays and pointers but I believe a `Span` would also be suitable. In fact, I think I can see an implementation already there, it's just marked as internal.

### API breakdown

```cs
public readonly ref partial struct Span2D
{
// ...
public Span2D(Span span, int height, int width) : this (span, 0, height, width, 0) {...}

public Span2D(Span span, int offset, int height, int width, int pitch) {...}
// ...
}
```

### Usage example

```cs
Span p = stackalloc int[9];
Span2D span = new Span2D(p, 3, 3);
```

### Breaking change?

No

### Alternatives

```cs
Span span = stackalloc int[6];
Span2D span2d = Span2D.DangerousCreate(ref span[0], 2, 3, 0);
```

```cs
int* p = stackalloc int[6];
Span2D span2d = new Span2D(p, 2, 3, 0);
```

### Additional context

_No response_

### Help us help you

Yes, but only if others can assist

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.