Turning on the views feature solves this problem.
{.experimental: "views".}
document: https://nim-lang.org/docs/manual_experimental.html#view-types
Sorry, this solution seems to have a strong side effect, it generates c code that calls autoToOpenArray twice. I can observe this phenomenon when I insert the echo statement in it.
Reproducible example: https://play.nim-lang.org/#ix=4EZ5
type
Slice*[T] = object
first, last: int
p: ptr UncheckedArray[T]
{.experimental: "views".}
converter autoToOpenArray*[T](s: Slice[T]): openArray[T] =
echo "here twice"
result = toOpenArray(s.p, s.first, s.last)
proc accpetOpenArray(s: openArray[byte]) = discard
proc main =
accpetOpenArray(Slice[byte]())
main()
output:
here twice
here twice
Slice is a bad name, it's already used for ranges.
Use Span or View https://github.com/nim-lang/RFCs/issues/32#issuecomment-373580383