diff --git a/Sources/Future/MutableSpan.swift b/Sources/Future/MutableSpan.swift index db1a3ed4..22c68574 100644 --- a/Sources/Future/MutableSpan.swift +++ b/Sources/Future/MutableSpan.swift @@ -76,37 +76,54 @@ extension MutableSpan where Element: ~Copyable { } } -//TODO: initializers where Element: BitwiseCopyable +@_disallowFeatureSuppression(NonescapableTypes) +extension MutableSpan where Element: BitwiseCopyable { -//extension MutableSpan /*where Element: BitwiseCopyable*/ { -// -// public init( //FIXME: should be failable -// unsafeMutableRawBufferPointer buffer: UnsafeMutableRawBufferPointer, -// dependsOn owner: borrowing Owner -// ) { -// guard _isPOD(Element.self) else { fatalError() } -// guard let p = buffer.baseAddress else { /*return nil*/ fatalError() } -// let (q, r) = buffer.count.quotientAndRemainder(dividingBy: MemoryLayout.stride) -// precondition(r == 0) -// self.init(unsafeMutablePointer: p.assumingMemoryBound(to: Element.self), count: q, dependsOn: owner) -// } -//} + @_disallowFeatureSuppression(NonescapableTypes) + @_alwaysEmitIntoClient + public init( + _unsafeElements buffer: UnsafeMutableBufferPointer + ) -> dependsOn(immortal) Self { + self.init(_unchecked: buffer) + } -//MARK: Sequence + @_disallowFeatureSuppression(NonescapableTypes) + @_alwaysEmitIntoClient + public init( + _unsafeStart start: UnsafeMutablePointer, + count: Int + ) -> dependsOn(immortal) Self { + precondition(count >= 0, "Count must not be negative") + self.init(_unchecked: start, count: count) + } -//extension MutableSpan /*: Sequence */ { -// -// public func makeIterator() -> SpanIterator { -// .init(from: 0, to: _count, dependsOn: /* self */ baseAddress) -// } -// -// //FIXME: mark closure parameter as non-escaping -// public func withContiguousStorageIfAvailable( -// _ body: (UnsafeBufferPointer) throws -> R -// ) rethrows -> R? { -// try Span(self).withContiguousStorageIfAvailable(body) -// } -//} + @_disallowFeatureSuppression(NonescapableTypes) + @_alwaysEmitIntoClient + public init( + _unsafeBytes buffer: UnsafeMutableRawBufferPointer + ) -> dependsOn(immortal) Self { + let (byteCount, stride) = (buffer.count, MemoryLayout.stride) + precondition(byteCount >= 0, "Count must not be negative") + let (count, remainder) = byteCount.quotientAndRemainder(dividingBy: stride) + precondition(remainder == 0) + _pointer = buffer.baseAddress?.assumingMemoryBound(to: Element.self) + _count = count + } + + @_disallowFeatureSuppression(NonescapableTypes) + @_alwaysEmitIntoClient + public init( + _unsafeStart pointer: UnsafeMutableRawPointer, + byteCount: Int + ) -> dependsOn(immortal) Self { + precondition(byteCount >= 0, "Count must not be negative") + let stride = MemoryLayout.stride + let (count, remainder) = byteCount.quotientAndRemainder(dividingBy: stride) + precondition(remainder == 0) + _pointer = pointer.assumingMemoryBound(to: Element.self) + _count = count + } +} @_disallowFeatureSuppression(NonescapableTypes) extension Span where Element: ~Copyable {