Skip to content

Commit 0fc4e4c

Browse files
author
Fahad Zubair
committed
Using ConcurrentHashmap just to be on the safe side
1 parent e2fdea9 commit 0fc4e4c

File tree

1 file changed

+14
-33
lines changed

1 file changed

+14
-33
lines changed

codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/RustCrateInlineModuleComposingWriter.kt

Lines changed: 14 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import java.util.concurrent.ConcurrentHashMap
1212
typealias DocWriter = () -> Any
1313
typealias InlineModuleCreator = (Symbol, Writable) -> Unit
1414

15-
1615
/**
1716
* Initializes RustCrate -> InnerModule data structure.
1817
*/
@@ -40,7 +39,7 @@ fun RustCrate.createInlineModuleCreator(): InlineModuleCreator {
4039

4140
/**
4241
* If the passed in `shape` is a synthetic extracted shape resulting from a constrained struct member,
43-
* the `Writable` is called using the structure's builder module. Otherwise the `Writable` is called
42+
* the `Writable` is called using the structure's builder module. Otherwise, the `Writable` is called
4443
* using the given `module`.
4544
*/
4645
fun RustCrate.withModuleOrWithStructureBuilderModule(
@@ -133,22 +132,15 @@ private val crateToInlineModule: ConcurrentHashMap<RustCrate, InnerModule> =
133132

134133
class InnerModule(debugMode : Boolean) {
135134
private val topLevelModuleWriters: MutableSet<RustWriter> = mutableSetOf()
136-
private val inlineModuleWriters: HashMap<RustWriter, MutableList<InlineModuleWithWriter>> = hashMapOf()
137-
private val docWriters: HashMap<RustModule.LeafModule, MutableList<DocWriter>> = hashMapOf()
135+
private val inlineModuleWriters: ConcurrentHashMap<RustWriter, MutableList<InlineModuleWithWriter>> = ConcurrentHashMap()
136+
private val docWriters: ConcurrentHashMap<RustModule.LeafModule, MutableList<DocWriter>> = ConcurrentHashMap()
138137
private val writerCreator = RustWriter.factory(debugMode)
139138
private val emptyLineCount: Int = writerCreator
140139
.apply("lines-it-always-writes.rs", "crate")
141140
.toString()
142141
.split("\n")[0]
143142
.length
144143

145-
private fun createNewInlineModule(): RustWriter {
146-
val writer = writerCreator.apply("unknown-module-would-never-be-written.rs", "crate")
147-
// There has never been a child of the top most inner module, hence create a new one
148-
inlineModuleWriters[writer] = mutableListOf()
149-
return writer
150-
}
151-
152144
fun withInlineModule(outerWriter: RustWriter, innerModule: RustModule.LeafModule, docWriter: DocWriter? = null, writable: Writable) {
153145
if (docWriter != null) {
154146
val moduleDocWriterList = docWriters.getOrPut(innerModule) { mutableListOf() }
@@ -220,6 +212,17 @@ class InnerModule(debugMode : Boolean) {
220212
withInlineModule(writer, bottomMost, docWriter, writable)
221213
}
222214

215+
/**
216+
* Creates an in memory writer and registers it with a map of RustWriter -> listOf(Inline descendent modules)
217+
*/
218+
private fun createNewInlineModule(): RustWriter {
219+
val writer = writerCreator.apply("unknown-module-would-never-be-written.rs", "crate")
220+
// Register the new RustWriter in the map to allow further descendent inline modules to be created inside it.
221+
inlineModuleWriters[writer] = mutableListOf()
222+
return writer
223+
}
224+
225+
223226
/**
224227
* Returns the complete hierarchy of a `RustModule.LeafModule` from top to bottom
225228
*/
@@ -316,28 +319,6 @@ class InnerModule(debugMode : Boolean) {
316319
}
317320
}
318321

319-
private fun combine(inlineModuleWithWriter: InlineModuleWithWriter, inlineModule: RustModule.LeafModule) : InlineModuleWithWriter {
320-
check(inlineModuleWithWriter.inlineModule.name == inlineModule.name) {
321-
"only inline module objects that have the same name can be combined"
322-
}
323-
check(inlineModuleWithWriter.inlineModule.rustMetadata.visibility == inlineModule.rustMetadata.visibility) {
324-
"only inline modules with same visibility can be combined"
325-
}
326-
check(inlineModuleWithWriter.inlineModule.inline && inlineModule.inline) {
327-
"both modules need to be inline to be combined together"
328-
}
329-
330-
val newModule = RustModule.new(
331-
name = inlineModule.name,
332-
parent = inlineModuleWithWriter.inlineModule.parent,
333-
documentation = inlineModuleWithWriter.inlineModule.documentation?.plus("\n")?.plus(inlineModule.documentation) ?: inlineModule.documentation,
334-
visibility = inlineModule.rustMetadata.visibility,
335-
additionalAttributes = inlineModuleWithWriter.inlineModule.rustMetadata.additionalAttributes + inlineModule.rustMetadata.additionalAttributes,
336-
inline = inlineModule.inline
337-
)
338-
return InlineModuleWithWriter(newModule, inlineModuleWithWriter.writer)
339-
}
340-
341322
private fun writeDocs(innerModule: RustModule.LeafModule) {
342323
docWriters[innerModule]?.forEach{
343324
it()

0 commit comments

Comments
 (0)