arennow / COW

A copy-on-write wrapper for Swift objects

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

COW

A copy-on-write wrapper for Swift objects

This is mainly useful for mutable objects that you need to pass around, but you want to [try really hard to] avoid shared mutability. For example

let doc = Cow(try XMLDocument(data: someData))

func getReadOnlyValue(doc: Cow<XMLDocument>) {
	print(doc.readOnlyObject.isStandalone)
}

func mutateTheDocument(doc: inout Cow<XMLDocument>) {
	doc.mutableObject.removeChild(at: 0)
}

Unfortunately, if you mutate the value you get from readOnlyObject, you're breaking the contract, and that change will be reflected by all the other copies of this COW. Since Swift isn't Rust, there's not a lot that can be done about that.

About

A copy-on-write wrapper for Swift objects

License:The Unlicense


Languages

Language:Swift 100.0%