I'm exploring ownership accessors by re-implementing Rust types in Swift.
public enum Cow<B : ToOwned & ~Copyable> : Borrow, Clone, ~Escapable {
/// Borrowed data.
case borrowed(Ref<B>)
/// Owned data.
case owned(B.Owned)
public var value: B {
borrow {
switch self {
case let .borrowed(b): b.value
case let .owned(b): b.value
}
}
mutate {
switch self {
case let .borrowed(b):
self = .owned(b.value.toOwned())
switch self {
case .owned(var o): return &o.value
default: unsafeBitCast((), to: Never.self)
}
case var .owned(o):
return &o.value
}
}
}
public func clone() -> Self {
switch self {
case let .borrowed(b): .borrowed(b)
case let .owned(o): .owned(o.value.toOwned())
}
}
mutating public func clone(from source: borrowing Cow) {
switch (self, source) {
case (var .owned(dest), let .owned(o)): o.value.clone(into: &dest)
default: self = source.clone()
}
}
}
I'm getting the following error on value: B on each return:
- Invalid return value from a mutate accessor
- Mutate accessors can return stored properties, computed properties with mutate accessors or global 'let' declarations
The .value refers to the computed property ({ borrow mutate }) in the Borrow protocol so I'd expect it to work.
What am I doing wrong?