Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Publish Segment.size and Segment.remainingCapacity #409

Merged
merged 1 commit into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions core/api/kotlinx-io-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ public final class kotlinx/io/Segment {
public final synthetic fun getNext ()Lkotlinx/io/Segment;
public final synthetic fun getPos ()I
public final synthetic fun getPrev ()Lkotlinx/io/Segment;
public final synthetic fun getRemainingCapacity ()I
public final synthetic fun getSize ()I
public final fun getRemainingCapacity ()I
public final fun getSize ()I
public final synthetic fun setLimit (I)V
public final synthetic fun setNext (Lkotlinx/io/Segment;)V
public final synthetic fun setPos (I)V
Expand Down
18 changes: 12 additions & 6 deletions core/common/src/Segment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -345,14 +345,20 @@ public class Segment {
limit += srcEndOffset - srcStartOffset
}

@PublishedApi
@get:JvmSynthetic
internal val size: Int
/**
* The number of readable bytes contained in this segment.
*
* @sample kotlinx.io.samples.unsafe.UnsafeBufferOperationsSamples.readUleb128
*/
public val size: Int
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps, it should be Long.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But then both pos and limit have to be updated too.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main issue here that these properties already leaked into users' projects, like Ktor.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And the reason why we might need Long properties here is supporting MemorySegmented backed segments on JVM (and that's nowhere near).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MemorySegments can be sliced which should be a cheap operation. If one came in over 2^31 in length, it could be sliced into multiple segments.

get() = limit - pos

@PublishedApi
@get:JvmSynthetic
internal val remainingCapacity: Int
/**
* The number of bytes that could be written into this segment.
*
* @sample kotlinx.io.samples.unsafe.UnsafeBufferOperationsSamples.writeUleb128Array
*/
public val remainingCapacity: Int
get() = data.size - limit

/**
Expand Down