Thu, 24 Apr 2003 09:54:01 +0100
> How would one implement an CStringLen-style type that
> (a) was efficient, in particular could be read and written to Handles
> (b) would get automatically deallocated by the Haskell=20
> garbage collector,
> when Haskell no longer referred to it;
> (c) was immutable, so that once created the character data could not
> be changed;
> (d) consequently had the property that conversion to and from=20
> String would
> be a pure operation;
> (e) could be passed to and from C using the FFI. (Of course=20
> C would need you
> to split the length and character data up; the character data=20
> would presumably
> have type "const char *".)
> It would be rather nice to have such a type.
Data.PackedString is *almost* what you want, and could be tweaked to do
the right thing (at least in GHC).
There are two problems: (1) the representation is currently as an array
of 32-bit unicode chars, whereas you probably want 8-bit ISO-8859 or
something. (2) Passing to FFI functions: to make this work you can use
pinned byte-arrays instead of ordinary byte-arrays to store the string,
and an explicit touch# after the FFI call.