-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Add CollectionsMarshal #26867
Add CollectionsMarshal #26867
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -0,0 +1,21 @@ | ||||
// Licensed to the .NET Foundation under one or more agreements. | ||||
// The .NET Foundation licenses this file to you under the MIT license. | ||||
// See the LICENSE file in the project root for more information. | ||||
|
||||
using System.Collections.Generic; | ||||
|
||||
namespace System.Runtime.InteropServices | ||||
{ | ||||
/// <summary> | ||||
/// An unsafe class that provides a set of methods to access the underlying data representations of collections. | ||||
/// </summary> | ||||
public static class CollectionsMarshal | ||||
{ | ||||
/// <summary> | ||||
/// Get a <see cref="Span{T}"/> view over a <see cref="List{T}"/>'s data. | ||||
/// Items should not be added or removed from the <see cref="List{T}"/> while the <see cref="Span{T}"/> is in use. | ||||
/// </summary> | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: We should get in the habit of adding docs for parameters and return values (effectively all the xml fields that docs require). Something to consider going forward for new API additions (of course this applies to all future PRs and not just this one since we'd like to emphasize adding docs right in time and not accrue tech debt). For example, see the following (which has param name and return value docs): See the following for an example: cc @mairaw, @carlossanlop Is there a way for us to detect and highlight the missing gaps in docs right in PRs to make it easier/discover-able for contributors? |
||||
public static Span<T> AsSpan<T>(List<T> list) | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need null argument validation here, don't we? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When writing the tests; I realised I'd prefer this arrangement public static Span<T> AsSpan<T>(List<T>? list)
=> new Span<T>(list?._items, 0, list?._size ?? 0); Not sure how people would feel about that? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is that better than There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems like the latter would have less null checks/etc There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's easier (at least for me) to reason about the code if written how @tannergooding suggested. I also would use public static Span<T> AsSpan<T>(List<T>? list)
=> list is null ? default : new Span<T>(list._items, 0, list._size); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe we prefer There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's easier to see call sites of We have
There are only a few hits of Although, a lot more for |
||||
=> new Span<T>(list._items, 0, list._size); | ||||
} | ||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it worth a comment that these fields are internal only for the use of CollectionsMarshal and to avoid further growing the generated code for List? It's not something we normally do.