Skip to content

Commit

Permalink
Merge pull request #24 from ydnar/ydnar/list-len
Browse files Browse the repository at this point in the history
cm: add List[T].Len() method
  • Loading branch information
ydnar authored Jan 16, 2024
2 parents b22d827 + 10fd81c commit 1e02558
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions cm/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,27 @@ import "unsafe"
// List represents a Component Model list<T>.
// The binary representation of list<T> is similar to a Go slice minus the cap field.
type List[T any] struct {
ptr *T
len uintptr
data *T
len uint
}

// ToList returns a List[T] equivalent to the Go slice s.
// The underlying slice data is not copied, and the resulting List points at the
// same array storage as the slice.
func ToList[S ~[]T, T any](s S) List[T] {
return List[T]{
ptr: unsafe.SliceData([]T(s)),
len: uintptr(len(s)),
data: unsafe.SliceData([]T(s)),
len: uint(len(s)),
}
}

// Len returns the length of the list.
// TODO: should this return an int instead of a uint?
func (list List[T]) Len() uint {
return uint(list.len)
}

// Slice returns a Go slice representing the List.
func (list List[T]) Slice() []T {
return unsafe.Slice(list.ptr, list.len)
return unsafe.Slice(list.data, list.len)
}

0 comments on commit 1e02558

Please sign in to comment.