-
Notifications
You must be signed in to change notification settings - Fork 240
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
Implement record type #243
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 |
---|---|---|
|
@@ -61,6 +61,7 @@ mod filters { | |
} | ||
Type::Optional(t) => format!("{}?", type_kt(t)?), | ||
Type::Sequence(t) => format!("List<{}>", type_kt(t)?), | ||
Type::Map(t) => format!("Map<String, {}>", type_kt(t)?), | ||
}) | ||
} | ||
|
||
|
@@ -124,6 +125,14 @@ mod filters { | |
calculate_write_size(&"v", t)?, | ||
write_kt(&"v", &"buf", t)? | ||
), | ||
Type::Map(t) => format!( | ||
"lowerMap({}, {{ k, v -> {} + {} }}, {{ k, v, buf -> {}; {} }})", | ||
nm, | ||
calculate_write_size(&"k", &Type::String)?, | ||
calculate_write_size(&"v", t)?, | ||
write_kt(&"k", &"buf", &Type::String)?, | ||
write_kt(&"v", &"buf", t)? | ||
), | ||
Comment on lines
+128
to
+135
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. 🤮 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. lol, but also, I wonder if something similar to what I tried for python over in #214 could be slightly less vomit-inducing here; thoughts? (for a future PR of course, not this one) 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. What I'm concerned about is that we would generate a lot of extra code for each new record/sequence/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. Yeah, I guess it's similar to the use of monomorphization in rust, where you end up generating lots of almost-duplicate code. (I don't think it would balloon too badly in practice, because you'd only get one method per type-you-actually-use-in-a-sequence, not one for every single type. But, worth watching out for). |
||
_ => format!("{}.lower()", nm), | ||
}) | ||
} | ||
|
@@ -151,6 +160,13 @@ mod filters { | |
target, | ||
write_kt(&"v", &"buf", t)? | ||
), | ||
Type::Map(t) => format!( | ||
"writeMap({}, {}, {{ k, v, buf -> {}; {} }})", | ||
nm, | ||
target, | ||
write_kt(&"k", &"buf", &Type::String)?, | ||
write_kt(&"v", &"buf", t)? | ||
), | ||
_ => format!("{}.write({})", nm, target), | ||
}) | ||
} | ||
|
@@ -175,6 +191,12 @@ mod filters { | |
nm, | ||
calculate_write_size(&"v", t)? | ||
), | ||
Type::Map(t) => format!( | ||
"calculateWriteSizeMap({}, {{ k, v -> {} + {} }})", | ||
nm, | ||
calculate_write_size(&"k", &Type::String)?, | ||
calculate_write_size(&"v", t)? | ||
), | ||
_ => format!("{}.calculateWriteSize()", nm), | ||
}) | ||
} | ||
|
@@ -192,6 +214,12 @@ mod filters { | |
Type::Sequence(t) => { | ||
format!("liftSequence({}, {{ buf -> {} }})", nm, read_kt(&"buf", t)?) | ||
} | ||
Type::Map(t) => format!( | ||
"liftMap({}, {{ buf -> Pair({}, {}) }})", | ||
nm, | ||
read_kt(&"buf", &Type::String)?, | ||
read_kt(&"buf", t)? | ||
), | ||
_ => format!("{}.lift({})", type_kt(type_)?, nm), | ||
}) | ||
} | ||
|
@@ -209,6 +237,12 @@ mod filters { | |
Type::Sequence(t) => { | ||
format!("readSequence({}, {{ buf -> {} }})", nm, read_kt(&"buf", t)?) | ||
} | ||
Type::Map(t) => format!( | ||
"readMap({}, {{ buf -> Pair({}, {}) }})", | ||
nm, | ||
read_kt(&"buf", &Type::String)?, | ||
read_kt(&"buf", t)? | ||
), | ||
_ => format!("{}.read({})", type_kt(type_)?, nm), | ||
}) | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -96,6 +96,21 @@ fun<T> readSequence(buf: ByteBuffer, readItem: (ByteBuffer) -> T): List<T> { | |
} | ||
} | ||
|
||
fun<V> liftMap(rbuf: RustBuffer.ByValue, readItem: (ByteBuffer) -> Pair<String, V>): Map<String, V> { | ||
return liftFromRustBuffer(rbuf) { buf -> readMap(buf, readItem) } | ||
} | ||
|
||
fun<V> readMap(buf: ByteBuffer, readItem: (ByteBuffer) -> Pair<String, V>): Map<String, V> { | ||
val len = Int.read(buf) | ||
@OptIn(ExperimentalStdlibApi::class) | ||
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. What effect will this opting-in to experiment stuff have on our consumers, if any? 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 should result in a warning during compilation, if we find this unacceptable we can always switch to a more stable construct. |
||
return buildMap<String, V>(len) { | ||
repeat(len) { | ||
val (k, v) = readItem(buf) | ||
put(k, v) | ||
} | ||
} | ||
} | ||
|
||
// Helpers for lowering primitive data types into a bytebuffer. | ||
// Since we need to allocate buffers from rust, the lowering process needs to be | ||
// able to calculate ahead-of-time what the required size of the buffer will be. | ||
|
@@ -244,6 +259,21 @@ fun<T> writeSequence(v: List<T>, buf: ByteBuffer, writeItem: (T, ByteBuffer) -> | |
v.forEach { writeItem(it, buf) } | ||
} | ||
|
||
fun<V> lowerMap(m: Map<String, V>, calculateWriteSize: (String, V) -> Int, writeEntry: (String, V, ByteBuffer) -> Unit): RustBuffer.ByValue { | ||
return lowerIntoRustBuffer(m, { m -> calculateWriteSizeMap(m, calculateWriteSize) }, { m, buf -> writeMap(m, buf, writeEntry) }) | ||
} | ||
|
||
fun<V> calculateWriteSizeMap(v: Map<String, V>, calculateWriteSize: (String, V) -> Int): Int { | ||
var len = v.size.calculateWriteSize() | ||
v.forEach { k, v -> len += calculateWriteSize(k, v) } | ||
return len | ||
} | ||
|
||
fun<V> writeMap(v: Map<String, V>, buf: ByteBuffer, writeEntry: (String, V, ByteBuffer) -> Unit) { | ||
v.size.write(buf) | ||
v.forEach { k, v -> writeEntry(k, v, buf) } | ||
} | ||
|
||
fun<T> lowerOptional(v: T?, calculateWriteSize: (T) -> Int, writeItem: (T, ByteBuffer) -> Unit): RustBuffer.ByValue { | ||
return lowerIntoRustBuffer(v, { v -> calculateWriteSizeOptional(v, calculateWriteSize) }, { v, buf -> writeOptional(v, buf, writeItem) }) | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -97,7 +97,13 @@ mod filters { | |
Type::Enum(type_name) => format!("{} = {}({})", nm, type_name, nm), | ||
Type::Record(type_name) => format!("{} = {}._coerce({})", nm, type_name, nm), | ||
Type::Optional(t) => format!("(None if {} is None else {})", nm, coerce_py(nm, t)?), | ||
Type::Sequence(t) => format!("({} for x in {})", coerce_py(&"x", t)?, nm), // TODO: name hygiene | ||
Type::Sequence(t) => format!("({} for x in {})", coerce_py(&"x", t)?, nm), // TODO: name hygiene, | ||
Type::Map(t) => format!( | ||
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 couldn't finish the python implementation, I think the other PR needs to land first. |
||
"({}:{} for (k, v) in {}.items())", | ||
coerce_py(&"k", t)?, | ||
coerce_py(&"v", t)?, | ||
nm | ||
), | ||
}) | ||
} | ||
|
||
|
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.
string
, so you have to declare aDOMString
instead (it still maps properly to a rustString
, we actually ignore the Key type).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.
I think this is an okay start, and we can figure out other key types as we go. Kinda surprised
weedle
wouldn't parsestring
, but oh well.