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

Lifetimes of inner references #3141

Merged
merged 2 commits into from
Nov 2, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,13 @@ inline fun <reified T : RustType.Container> RustType.stripOuter(): RustType = wh
else -> this
}

/** Extracts the inner Reference type */
fun RustType.innerReference(): RustType? = when (this) {
is RustType.Reference -> this
is RustType.Container -> this.member.innerReference()
else -> null
}

/** Wraps a type in Option if it isn't already */
fun RustType.asOptional(): RustType = when (this) {
is RustType.Option -> this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import software.amazon.smithy.rust.codegen.core.rustlang.asRef
import software.amazon.smithy.rust.codegen.core.rustlang.deprecatedShape
import software.amazon.smithy.rust.codegen.core.rustlang.docs
import software.amazon.smithy.rust.codegen.core.rustlang.documentShape
import software.amazon.smithy.rust.codegen.core.rustlang.innerReference
import software.amazon.smithy.rust.codegen.core.rustlang.isCopy
import software.amazon.smithy.rust.codegen.core.rustlang.isDeref
import software.amazon.smithy.rust.codegen.core.rustlang.render
Expand Down Expand Up @@ -93,7 +94,7 @@ open class StructureGenerator(
*/
private fun lifetimeDeclaration(): String {
val lifetimes = members
.map { symbolProvider.toSymbol(it).rustType() }
.map { symbolProvider.toSymbol(it).rustType().innerReference() }
.mapNotNull {
when (it) {
is RustType.Reference -> it.lifetime
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,4 +220,13 @@ internal class RustTypesTest {
val attributeMacro = Attribute(derive())
forInputExpectOutput(writable { attributeMacro.render(this) }, "")
}

@Test
fun `finds inner reference type`() {
val innerReference = RustType.Reference("a", RustType.Bool)
val type = RustType.Box(RustType.Option(innerReference))

type.innerReference() shouldBe innerReference
RustType.Bool.innerReference() shouldBe null
}
}