String.Cap
Capabilities for strings.
This modules provides the same set of features as String
, but with the added twist that strings can be made read-only or write-only. Read-only strings may then be safely shared and distributed.
module changed to accommodate the -safe-string transition. OCaml now uses two distinct types for mutable and immutable string, which is a good design but is not as expressive as the present Cap interface, and actually makes implementing Cap harder than it previously was. We are aware that current state is not optimal for heavy Cap users; if you are one of them, please get in touch (on the Batteries issue tracker for example) so that we can discuss code refactoring and improvements for this sub-module.
The type of capability strings.
If 'a
contains [`Read]
, the contents of the string may be read. If 'a
contains [`Write]
, the contents of the string may be written.
Other (user-defined) capabilities may be added without loss of performance or features. For instance, a string could be labelled [`Read | `UTF8]
to state that it contains UTF-8 encoded data and may be used only for reading. Conversely, a string labelled with []
(i.e. nothing) can neither be read nor written. It can only be compared for textual equality using OCaml's built-in compare
or for physical equality using OCaml's built-in ==
.
val length : _ t -> int
val is_empty : _ t -> bool
val get : [> `Read ] t -> int -> char
val set : [> `Write ] t -> int -> char -> unit
val create : int -> _ t
Adopt a regular byte sequence.
One could give a perfectly safe semantics to an of_string : string -> _ t
function, but this requires making a copy of the string. Previous versions of this interface advertised the absence of performance overhead, so it's better to warn the user and let them decide (through the use of either Bytes.of_string or Bytes.unsafe_of_string) whether they can safely avoid a copy or need to insert one.
Adopt a regular byte sequence.
Note that adopting a byte sequence, even at the restrictive `Read
type, does not make a copy. Having a `Read
string prevents you (and anyone you pass it to) from writing it, but your parent may have knowledge of the string at a more permissive type and perform writes on it.
If you want to use a `Read
string and assume it will not get written to, you should either properly "adopt" it by ensuring unique ownership (this cannot be guaranteed by the type system), or make a copy of it at adoption time: Cap.of_bytes
(Bytes.copy buf)
.
Return a capability string as a regular byte sequence.
We cannot return a string
here, and it would be incorrect to do so even if we required [< `Read] t
as input. Indeed, one can start from a writeable byte sequence, and then use the read_only
function below to cast it into a [`Read]
t
. Capabilities are used to enforce local protocol (only reads, only writes, both reads and writes...), they don't guarantee that other users of the same (shared) value all follow the same protocol. To safely reason about mutability one needs stronger ownership guarantees.
If you want to obtain an immutable string
out of a capability string, you should first convert it to a mutable byte sequence and then copy it into an immutable string. If you have extra knowledge about the ownership of the value, you may use unsafe conversion functions to avoid the copy, see the documentation of unsafe conversion functions.
Return a capability string as a regular byte sequence.
val make : int -> char -> _ t
val init : int -> (int -> char) -> _ t
val of_list : char list -> _ t
val to_list : [> `Read ] t -> char list
val of_int : int -> _ t
val of_float : float -> _ t
val of_char : char -> _ t
val to_int : [> `Read ] t -> int
val to_float : [> `Read ] t -> float
val fold_left : ('a -> char -> 'a) -> 'a -> [> `Read ] t -> 'a
val fold_lefti : ('a -> int -> char -> 'a) -> 'a -> [> `Read ] t -> 'a
val fold_right : (char -> 'a -> 'a) -> [> `Read ] t -> 'a -> 'a
val fold_righti : (int -> char -> 'a -> 'a) -> [> `Read ] t -> 'a -> 'a
val iter : (char -> unit) -> [> `Read ] t -> unit
val index : [> `Read ] t -> char -> int
val rindex : [> `Read ] t -> char -> int
val index_from : [> `Read ] t -> int -> char -> int
val rindex_from : [> `Read ] t -> int -> char -> int
val contains : [> `Read ] t -> char -> bool
val contains_from : [> `Read ] t -> int -> char -> bool
val rcontains_from : [> `Read ] t -> int -> char -> bool
val count_char : [> `Read ] t -> char -> int
val quote : [> `Read ] t -> string
val fill : [> `Write ] t -> int -> int -> char -> unit
val explode : [> `Read ] t -> char list
val implode : char list -> _ t
val print : 'a BatInnerIO.output -> [> `Read ] t -> unit
val println : 'a BatInnerIO.output -> [> `Read ] t -> unit
val print_quoted : 'a BatInnerIO.output -> [> `Read ] t -> unit
module Exceptionless : sig ... end
Exceptionless counterparts for error-raising operations