Last active
March 27, 2019 16:53
-
-
Save chriseidhof/d96f0f652a7c6358d865 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// main.swift | |
// Test | |
// | |
// Created by Chris Eidhof on 29/08/15. | |
// Copyright © 2015 Chris Eidhof. All rights reserved. | |
// | |
import Foundation | |
import Quartz | |
final class Box<A> { | |
let unbox: A | |
init(_ value: A) { unbox = value } | |
} | |
struct Data { | |
private var boxedData: Box<NSMutableData> | |
var data: NSData { return boxedData.unbox } | |
init(data: NSData = NSData()) { | |
self.boxedData = Box(NSMutableData(data: data)) | |
} | |
} | |
/// Immutable | |
extension Data { | |
var length: Int { return data.length } | |
var bytes: UnsafePointer<Void> { | |
return data.bytes | |
} | |
} | |
/// Mutating | |
extension Data { | |
private var mutableData: NSMutableData { | |
mutating get { | |
if !isUniquelyReferencedNonObjC(&boxedData) { | |
boxedData = Box(NSMutableData(data: data)) | |
} | |
return boxedData.unbox | |
} | |
} | |
mutating func append(other: NSData) { | |
mutableData.appendData(other) | |
} | |
} | |
var data = Data(data: NSData()) | |
data.append(NSData()) | |
print(data.data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment