Skip to content

Commit

Permalink
Rust: generate <integer>::from_str_radix for non-base-10 .to_i conver…
Browse files Browse the repository at this point in the history
…sions
  • Loading branch information
Mingun committed Mar 27, 2024
1 parent 945b6cc commit e7e708b
Showing 1 changed file with 7 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,17 @@ class RustTranslator(provider: TypeProvider, config: RuntimeConfig) extends Base
override def strConcat(left: expr, right: expr, extPrec: Int) =
"format!(\"{}{}\", " + translate(left) + ", " + translate(right) + ")"

override def strToInt(s: expr, base: expr): String =
translate(base) match {
// TODO: do not generate .unwrap(), generate ? instead
override def strToInt(s: expr, base: expr): String = {
val baseStr = translate(base)
baseStr match {
case "10" =>
s"${translate(s)}.parse().unwrap()"
case _ =>
"panic!(\"Converting from string to int in base {} is unimplemented\", " + translate(base) + ")"
// TODO: Would be better to add a method to runtime which would be able to deduce returning type
s"i64::from_str_radix(${translate(s)}, $baseStr).unwrap()"
}
}

override def enumToInt(v: expr, et: EnumType): String =
translate(v)
Expand Down

0 comments on commit e7e708b

Please sign in to comment.