Skip to content

Commit 3aa310e

Browse files
committed
grpc-native: Refactor WireDecoder to avoid boxing nullable primitives
Signed-off-by: Johannes Zottele <[email protected]>
1 parent 5f80a26 commit 3aa310e

File tree

3 files changed

+133
-145
lines changed

3 files changed

+133
-145
lines changed

grpc/grpc-core/src/commonMain/kotlin/kotlinx/rpc/grpc/internal/WireDecoder.kt

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,11 @@ import kotlinx.io.Buffer
1111
*
1212
* This decoder is used by first calling [readTag], than looking up the field based on the field number in the returned,
1313
* tag and then calling the actual `read*()` method to read the value to the corresponding field.
14-
* This means that the nullable return value does not collide with optional fields, as optional fields would not
15-
* include a tag in the encoded message.
1614
*
17-
* If one `read*()` method returns `null`, decoding the data failed and no further
18-
* decoding can be done.
15+
* [hadError] indicates an error during decoding. While calling `read*()` is safe, the returned values
16+
* are meaningless if [hadError] returns `true`.
1917
*
20-
* NOTE: If the value of a `read*()` method is non-null, it doesn't mean that the
18+
* NOTE: If the [hadError] after a call to `read*()` returns `false`, it doesn't mean that the
2119
* value is correctly decoded. E.g., the following test will pass:
2220
* ```kt
2321
* val fieldNr = 1
@@ -29,43 +27,45 @@ import kotlinx.io.Buffer
2927
*
3028
* WireDecoder(buffer).use { decoder ->
3129
* decoder.readTag()
32-
* assertNotNull(decoder.readBool())
30+
* decoder.readBool()
31+
* assertFalse(decoder.hasError())
3332
* }
3433
* ```
3534
*/
3635
internal interface WireDecoder : AutoCloseable {
36+
fun hadError(): Boolean
3737
fun readTag(): KTag?
38-
fun readBool(): Boolean?
39-
fun readInt32(): Int?
40-
fun readInt64(): Long?
41-
fun readUInt32(): UInt?
42-
fun readUInt64(): ULong?
43-
fun readSInt32(): Int?
44-
fun readSInt64(): Long?
45-
fun readFixed32(): UInt?
46-
fun readFixed64(): ULong?
47-
fun readSFixed32(): Int?
48-
fun readSFixed64(): Long?
49-
fun readFloat(): Float?
50-
fun readDouble(): Double?
38+
fun readBool(): Boolean
39+
fun readInt32(): Int
40+
fun readInt64(): Long
41+
fun readUInt32(): UInt
42+
fun readUInt64(): ULong
43+
fun readSInt32(): Int
44+
fun readSInt64(): Long
45+
fun readFixed32(): UInt
46+
fun readFixed64(): ULong
47+
fun readSFixed32(): Int
48+
fun readSFixed64(): Long
49+
fun readFloat(): Float
50+
fun readDouble(): Double
5151

52-
fun readEnum(): Int?
53-
fun readString(): String?
54-
fun readBytes(): ByteArray?
55-
fun readPackedBool(): List<Boolean>?
56-
fun readPackedInt32(): List<Int>?
57-
fun readPackedInt64(): List<Long>?
58-
fun readPackedSInt32(): List<Int>?
59-
fun readPackedSInt64(): List<Long>?
60-
fun readPackedUInt32(): List<UInt>?
61-
fun readPackedUInt64(): List<ULong>?
62-
fun readPackedFixed32(): List<UInt>?
63-
fun readPackedFixed64(): List<ULong>?
64-
fun readPackedSFixed32(): List<Int>?
65-
fun readPackedSFixed64(): List<Long>?
66-
fun readPackedFloat(): List<Float>?
67-
fun readPackedDouble(): List<Double>?
68-
fun readPackedEnum(): List<Int>?
52+
fun readEnum(): Int
53+
fun readString(): String
54+
fun readBytes(): ByteArray
55+
fun readPackedBool(): List<Boolean>
56+
fun readPackedInt32(): List<Int>
57+
fun readPackedInt64(): List<Long>
58+
fun readPackedSInt32(): List<Int>
59+
fun readPackedSInt64(): List<Long>
60+
fun readPackedUInt32(): List<UInt>
61+
fun readPackedUInt64(): List<ULong>
62+
fun readPackedFixed32(): List<UInt>
63+
fun readPackedFixed64(): List<ULong>
64+
fun readPackedSFixed32(): List<Int>
65+
fun readPackedSFixed64(): List<Long>
66+
fun readPackedFloat(): List<Float>
67+
fun readPackedDouble(): List<Double>
68+
fun readPackedEnum(): List<Int>
6969
}
7070

7171
/**

0 commit comments

Comments
 (0)