
How would one implement an CStringLen-style type that (a) was efficient, in particular could be read and written to Handles efficiently; (b) would get automatically deallocated by the Haskell 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 String would be a pure operation; (e) could be passed to and from C using the FFI. (Of course C would need you to split the length and character data up; the character data 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. Cheers, Simon