@ -152,6 +152,44 @@ func ContainerViewOf[T views.ViewCloner[T, V], V views.StructView[T]](c *Contain
return ContainerView [ T , V ] { c }
return ContainerView [ T , V ] { c }
}
}
// MapContainer is a predefined map-like container type.
// Unlike [Container], it has two type parameters, where the value
// is the second parameter.
type MapContainer [ K comparable , V views . Cloner [ V ] ] struct {
Items map [ K ] V
}
func ( c * MapContainer [ K , V ] ) Clone ( ) * MapContainer [ K , V ] {
if c == nil {
return nil
}
var m map [ K ] V
if c . Items != nil {
m = make ( map [ K ] V , len ( c . Items ) )
for i := range m {
m [ i ] = c . Items [ i ] . Clone ( )
}
}
return & MapContainer [ K , V ] { m }
}
// MapContainerView is a pre-defined readonly view of a [MapContainer][K, T].
type MapContainerView [ K comparable , T views . ViewCloner [ T , V ] , V views . StructView [ T ] ] struct {
// ж is the underlying mutable value, named with a hard-to-type
// character that looks pointy like a pointer.
// It is named distinctively to make you think of how dangerous it is to escape
// to callers. You must not let callers be able to mutate it.
ж * MapContainer [ K , T ]
}
func ( cv MapContainerView [ K , T , V ] ) Items ( ) views . MapFn [ K , T , V ] {
return views . MapFnOf ( cv . ж . Items , func ( t T ) V { return t . View ( ) } )
}
func MapContainerViewOf [ K comparable , T views . ViewCloner [ T , V ] , V views . StructView [ T ] ] ( c * MapContainer [ K , T ] ) MapContainerView [ K , T , V ] {
return MapContainerView [ K , T , V ] { c }
}
type GenericBasicStruct [ T BasicType ] struct {
type GenericBasicStruct [ T BasicType ] struct {
Value T
Value T
}
}
@ -160,5 +198,7 @@ type StructWithContainers struct {
IntContainer Container [ int ]
IntContainer Container [ int ]
CloneableContainer Container [ * StructWithPtrs ]
CloneableContainer Container [ * StructWithPtrs ]
BasicGenericContainer Container [ GenericBasicStruct [ int ] ]
BasicGenericContainer Container [ GenericBasicStruct [ int ] ]
ClonableGenericContainer Container [ * GenericNoPtrsStruct [ int ] ]
CloneableGenericContainer Container [ * GenericNoPtrsStruct [ int ] ]
CloneableMap MapContainer [ int , * StructWithPtrs ]
CloneableGenericMap MapContainer [ int , * GenericNoPtrsStruct [ int ] ]
}
}