iter: make safer by using NdbNote instead of unsafe pointers

If we have an owned note, we could lose track of the lifetime and then
crash. Let's make sure we always have an NdbNote instead
This commit is contained in:
William Casarin
2023-07-22 16:56:13 -07:00
parent af7ea7024f
commit 58e2fb40ef
4 changed files with 35 additions and 13 deletions

View File

@@ -8,9 +8,21 @@
import Foundation
struct TagSequence: Sequence {
let note: UnsafeMutablePointer<ndb_note>
let note: NdbNote
let tag: UnsafeMutablePointer<ndb_tag>
var count: UInt16 {
tag.pointee.count
}
subscript(index: Int) -> NdbTagElem? {
if index >= tag.pointee.count {
return nil
}
return NdbTagElem(note: note, tag: tag, index: Int32(index))
}
func makeIterator() -> TagIterator {
return TagIterator(note: note, tag: tag)
}
@@ -29,10 +41,14 @@ struct TagIterator: IteratorProtocol {
}
var index: Int32
let note: UnsafeMutablePointer<ndb_note>
let note: NdbNote
var tag: UnsafeMutablePointer<ndb_tag>
init(note: UnsafeMutablePointer<ndb_note>, tag: UnsafeMutablePointer<ndb_tag>) {
var count: UInt16 {
tag.pointee.count
}
init(note: NdbNote, tag: UnsafeMutablePointer<ndb_tag>) {
self.note = note
self.tag = tag
self.index = 0