Skip to content

Conversation

@konard
Copy link
Member

@konard konard commented Sep 14, 2025

Summary

This PR addresses issue #55 by making the Segment<T> class semantically compatible with .NET's ArraySegment<T> while maintaining full backward compatibility.

Key Changes Made:

  • Static Empty property: Provides an empty segment instance similar to ArraySegment<T>.Empty
  • Array property: Returns the underlying array when Base is of type T[], compatible with ArraySegment<T>.Array
  • ArraySegment-compatible constructors:
    • Segment(T[] array) - creates a segment covering the entire array
    • Segment(T[] array, int offset, int count) - creates a segment with bounds checking
  • Slice() methods: Create sub-segments from existing segments (both single-parameter and two-parameter overloads)
  • ToArray() method: Copies segment contents to a new array

Backward Compatibility

  • All existing functionality remains unchanged
  • Existing constructors and methods work exactly as before
  • The Count property continues to work as an alias for Length (IList requirement)

Testing

  • Added comprehensive test suite with 13 test methods covering:

    • Empty segment creation
    • Array-based constructors with validation
    • Slice operations with boundary checking
    • ToArray functionality
    • ArraySegment semantic compatibility
    • Property access and enumeration
  • All existing tests continue to pass (31 total tests)

  • New implementation handles edge cases and provides appropriate error messages

Implementation Details

The implementation maintains the original design of working with any IList<T> while adding specific optimizations and compatibility features for arrays. The new Array property returns null when the underlying collection is not an array, which matches the behavior users would expect from ArraySegment-style APIs.

🤖 Generated with Claude Code


Resolves #55

Adding CLAUDE.md with task information for AI processing.
This file will be removed when the task is complete.

Issue: #55
@konard konard self-assigned this Sep 14, 2025
Added ArraySegment<T>-compatible API to Segment<T> class:
- Static Empty property for creating empty segments
- Array property that returns the underlying array when Base is T[]
- Constructors that match ArraySegment<T> signature patterns
- Slice() methods for creating sub-segments
- ToArray() method for copying segment contents to a new array

All changes maintain backward compatibility while adding new functionality.
Comprehensive test coverage included.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
@konard konard changed the title [WIP] Make the semantic of Segment as close to ArraySegment as possible Make Segment<T> semantics compatible with ArraySegment<T> Sep 14, 2025
@konard konard marked this pull request as ready for review September 14, 2025 04:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make the semantic of Segment as close to ArraySegment as possible

2 participants