@@ -20,14 +20,22 @@ public class DocumentStorage {
2020 return Int ( bson_count_keys ( self . pointer) )
2121 }
2222
23+ /// Initializes a new, empty `DocumentStorage`.
2324 internal init ( ) {
2425 self . pointer = bson_new ( )
2526 }
2627
27- internal init ( fromPointer pointer: BSONPointer ) {
28+ /// Initializes a new `DocumentStorage` by copying over the data from the provided `bson_t`.
29+ internal init ( copying pointer: BSONPointer ) {
2830 self . pointer = bson_copy ( pointer)
2931 }
3032
33+ /// Initializes a new `DocumentStorage` that uses the provided `bson_t` as its backing storage.
34+ /// The newly created instance will handle cleaning up the pointer upon deinitialization.
35+ internal init ( stealing pointer: MutableBSONPointer ) {
36+ self . pointer = pointer
37+ }
38+
3139 /// Cleans up internal state.
3240 deinit {
3341 guard let pointer = self . pointer else {
@@ -57,17 +65,30 @@ extension Document {
5765 }
5866
5967 /**
60- * Initializes a `Document` from a pointer to a bson_t. Uses a copy
61- * of `bsonData`, so the caller is responsible for freeing the original
62- * memory.
68+ * Initializes a `Document` from a pointer to a `bson_t` by making a copy of the data. The `bson_t`'s owner is
69+ * responsible for freeing the original.
70+ *
71+ * - Parameters:
72+ * - pointer: a BSONPointer
73+ *
74+ * - Returns: a new `Document`
75+ */
76+ internal init ( copying pointer: BSONPointer ) {
77+ self . storage = DocumentStorage ( copying: pointer)
78+ self . count = self . storage. count
79+ }
80+
81+ /**
82+ * Initializes a `Document` from a pointer to a `bson_t`, "stealing" the `bson_t` to use for underlying storage/
83+ * The `bson_t` must not be modified or freed by others after it is used here/
6384 *
6485 * - Parameters:
65- * - fromPointer : a BSONPointer
86+ * - pointer : a MutableBSONPointer
6687 *
6788 * - Returns: a new `Document`
6889 */
69- internal init ( fromPointer pointer: BSONPointer ) {
70- self . storage = DocumentStorage ( fromPointer : pointer)
90+ internal init ( stealing pointer: MutableBSONPointer ) {
91+ self . storage = DocumentStorage ( stealing : pointer)
7192 self . count = self . storage. count
7293 }
7394
@@ -232,7 +253,7 @@ extension Document {
232253 */
233254 private mutating func copyStorageIfRequired( ) {
234255 if !isKnownUniquelyReferenced( & self . storage) {
235- self . storage = DocumentStorage ( fromPointer : self . data)
256+ self . storage = DocumentStorage ( copying : self . data)
236257 }
237258 }
238259
@@ -319,7 +340,7 @@ extension Document {
319340 * - A `UserError.invalidArgumentError` if the data passed in is invalid JSON.
320341 */
321342 public init ( fromJSON: Data ) throws {
322- self . storage = DocumentStorage ( fromPointer : try fromJSON. withUnsafeBytes { ( bytes: UnsafePointer < UInt8 > ) in
343+ self . storage = DocumentStorage ( stealing : try fromJSON. withUnsafeBytes { ( bytes: UnsafePointer < UInt8 > ) in
323344 var error = bson_error_t ( )
324345 guard let bson = bson_new_from_json ( bytes, fromJSON. count, & error) else {
325346 if error. domain == BSON_ERROR_JSON {
@@ -328,11 +349,7 @@ extension Document {
328349 throw RuntimeError . internalError ( message: toErrorString ( error) )
329350 }
330351
331- #if compiler(>=5.0)
332352 return bson
333- #else
334- return UnsafePointer ( bson)
335- #endif
336353 } )
337354 self . count = self . storage. count
338355 }
@@ -348,7 +365,7 @@ extension Document {
348365
349366 /// Constructs a `Document` from raw BSON `Data`.
350367 public init ( fromBSON: Data ) {
351- self . storage = DocumentStorage ( fromPointer : fromBSON. withUnsafeBytes { ( bytes: UnsafePointer < UInt8 > ) in
368+ self . storage = DocumentStorage ( stealing : fromBSON. withUnsafeBytes { ( bytes: UnsafePointer < UInt8 > ) in
352369 bson_new_from_data ( bytes, fromBSON. count)
353370 } )
354371 self . count = self . storage. count
@@ -456,7 +473,7 @@ extension Document: BSONValue {
456473 throw RuntimeError . internalError ( message: " Failed to create a Document from iterator " )
457474 }
458475
459- return self . init ( fromPointer : docData)
476+ return self . init ( stealing : docData)
460477 }
461478 }
462479}
0 commit comments