That's very close, yes! I would suggest a few very subtle refinements:
struct Container {
// Note that this is optional: the weak property can be nil
// because its referent has been destroyed, but it can also just
// be nil because you stored nil into it.
var c_storage: WeakReference<C>?
var c: C? {
get {
guard let ref = c.storage else { return nil }
return ref.isDeallocated ? nil : ref.object
}
set {
if let newObject = newValue {
// Note that we make a new reference instead of
// modifying the existing one. `Container` still has
// value semantics: properties of copies of the same
// value have to be independent.
c_storage = WeakReference(object: newObject)
} else {
c_storage = nil
}
}
}
}