How do I constraint types in generic function?

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

@nikitamounier thanks
Your way much simpler than I.

1 Like

Won't the returned pointer be useless / pointing into the no longer existing cString?

1 Like

Right, the pointer produced by withUnsafeMutableBytes is only valid inside the closure.

Terms of Service

Privacy Policy

Cookie Policy