Tuming
(Tuming)
August 19, 2021, 4:07am
1
How do I constraint makeUnsafeMutablePointer
function only support UInt8 and Int8 types?
I also try
makeUnsafeMutablePointer<T: UInt8, T: UInt8>(::)
and
makeUnsafeMutablePointer(::) -> UnsafeMutablePointer<T> where T:...
but, they are not work.
extension String {
public func makeUnsafeMutablePointer<T>(forType type: T.Type, using encoding: String.Encoding = .utf8) -> UnsafeMutablePointer<T> {
var cString = self.cString(using: encoding)
return cString!.withUnsafeMutableBytes { $0.baseAddress!.bindMemory(to: type, capacity: $0.count) }
}
}
Why not just make two different overloads of your function, one using Uint8 and one using Int8? That seems simpler than using generics.
extension String {
public func makeUnsafeMutablePointer(forType: UInt8.Type, using encoding: String.Encoding = .utf8) -> UnsafeMutablePointer<UInt8> {
var cString = self.cString(using: encoding)
return cString!.withUnsafeMutableBytes { $0.baseAddress!.bindMemory(to: UInt8.self, capacity: $0.count) }
}
public func makeUnsafeMutablePointer(forType: Int8.Type, using encoding: String.Encoding = .utf8) -> UnsafeMutablePointer<Int8> {
var cString = self.cString(using: encoding)
return cString!.withUnsafeMutableBytes { $0.baseAddress!.bindMemory(to: UInt8.self, capacity: $0.count) }
}
}
I still kept your forType
parameter so that there isn't any ambiguity in the type-checker for which overload to pick from.
3 Likes
Tuming
(Tuming)
August 19, 2021, 8:05am
3
@nikitamounier thanks
Your way much simpler than I.
1 Like
Jens
August 19, 2021, 7:57pm
4
Won't the returned pointer be useless / pointing into the no longer existing cString
?
1 Like
scanon
(Steve Canon)
August 19, 2021, 8:12pm
5
Right, the pointer produced by withUnsafeMutableBytes
is only valid inside the closure.