@@ -150,6 +150,7 @@ export abstract class BaseTypeGenerator<Type extends RustType> {
150
150
let nativeCallPrefix = '' ;
151
151
let nativeCallWrapperPrefix = '' , nativeCallWrapperSuffix = '' ;
152
152
let nativeCallSuffix = '' ;
153
+ const anchors = [ ] ;
153
154
154
155
const semantics = this . methodSemantics ( method , containerType ) ;
155
156
if ( semantics . isConstructor && forceStaticConstructor ) {
@@ -193,6 +194,9 @@ export abstract class BaseTypeGenerator<Type extends RustType> {
193
194
nativeCallWrapperSuffix += preparedArgument . methodCallWrapperSuffix ;
194
195
nativeCallValueAccessors . push ( preparedArgument . accessor ) ;
195
196
nativeCallSuffix += preparedArgument . deferredCleanup ;
197
+ if ( preparedArgument . requiresAnchoring ) {
198
+ anchors . push ( swiftArgumentName ) ;
199
+ }
196
200
}
197
201
198
202
let cloneabilityDeprecationWarning = '' ;
@@ -226,11 +230,13 @@ export abstract class BaseTypeGenerator<Type extends RustType> {
226
230
visibility = 'internal' ;
227
231
}
228
232
233
+ let anchoringCommand = anchors . map ( a => `try! returnValue.addAnchor(anchor: ${ a } )\n` ) . join ( '\t\t\t\t\t\t' ) ;
229
234
let methodDeclarationKeywords = `${ visibility } ${ staticInfix } func` ;
230
- let returnCommand = ' return returnValue' ;
235
+ let returnCommand = ` ${ anchoringCommand } return returnValue` ;
231
236
let returnValueHandlingPrefix = '' ;
232
237
let returnValueHandlingSuffix = '' ;
233
238
if ( swiftMethodName === 'init' ) {
239
+ anchoringCommand = anchoringCommand . replaceAll ( 'returnValue.addAnchor(' , 'self.addAnchor(' )
234
240
// it's a constructor
235
241
methodDeclarationKeywords = visibility ;
236
242
returnCommand = `
@@ -239,6 +245,7 @@ export abstract class BaseTypeGenerator<Type extends RustType> {
239
245
Self.instanceCounter += 1
240
246
self.instanceNumber = Self.instanceCounter
241
247
super.init(conflictAvoidingVariableName: 0)
248
+ ${ anchoringCommand }
242
249
` ;
243
250
244
251
returnValueHandlingPrefix = '/*' ;
@@ -625,7 +632,8 @@ export abstract class BaseTypeGenerator<Type extends RustType> {
625
632
methodCallWrapperPrefix : '' ,
626
633
methodCallWrapperSuffix : '' ,
627
634
628
- deferredCleanup : ''
635
+ deferredCleanup : '' ,
636
+ requiresAnchoring : false
629
637
} ;
630
638
631
639
// this argument is the content of an elided container, like the iteratee of a Vec
@@ -807,6 +815,10 @@ export abstract class BaseTypeGenerator<Type extends RustType> {
807
815
}
808
816
` ;
809
817
818
+ if ( memoryContext && memoryContext . isConstructor && ( argument . type instanceof RustStruct || argument . type instanceof RustTaggedValueEnum || argument . type instanceof RustResult ) ) {
819
+ preparedArgument . requiresAnchoring = true ;
820
+ }
821
+
810
822
// the wrapper accesses the variable normally, and introduces a new variable name by which to refer to the value
811
823
preparedArgument . accessor = preparedArgument . name ;
812
824
}
@@ -1284,6 +1296,11 @@ export interface PreparedArgument {
1284
1296
* memory deallocation or, ironically, retention beyond the call site
1285
1297
*/
1286
1298
deferredCleanup : string
1299
+
1300
+ /**
1301
+ * If true (only applicable in constructors), add a `self.addAnchor` call after super.init()
1302
+ */
1303
+ requiresAnchoring : boolean
1287
1304
}
1288
1305
1289
1306
export interface PreparedReturnValue {
0 commit comments