pub enum ClipboardContent {
Bytes(Vec<u8>),
Empty,
Unavailable,
}Expand description
What a clipboard read produced.
§Decision: three outcomes, because two of them were being conflated
This started life as Option<Vec<u8>>, on the argument that “the clipboard
was empty”, “it held no compatible format” and “the transfer failed” all
mean there is nothing to paste and a caller does the same thing for each.
P0.5c found that wrong against a real compositor, in two ways that a caller
genuinely must tell apart:
- A successful transfer of zero bytes is not a failure. A peer may
publish an empty selection, and
Bytes(vec![])says so. ReportingNonethere tells an editor its paste failed when it did not. - “Could not be read” is not “there is nothing there”. A transfer whose peer went away mid-write, or which never became readable at all, is a failure a UI may want to surface (“the clipboard could not be read”) and which an automation script must not silently treat as an empty clipboard.
Empty still merges “the clipboard holds nothing” with “it
holds nothing in that format”, because those two really are one case: the
caller named the format, and the answer is that it is not on offer. A caller
that wants a different format asks for it, which is one more request either
way.
What is deliberately not here is “not yet”. A read that cannot be
answered yet is held until it can be — see
clipboard_request — because a variant
meaning “ask again later” is a retry loop in every consumer, and the
consumer has no idea how long to wait.
Variants§
Bytes(Vec<u8>)
The payload, in the format ClipboardData::mime
names. May be empty: that is a successful transfer of nothing.
Empty
The clipboard holds nothing, or nothing in the requested format.
The read could not be completed.
The window system refused it, the peer holding the data went away or
never delivered, or the window never became able to read the clipboard
within the backend’s deadline. Distinct from Empty:
there may well be something on the clipboard.
Implementations§
Source§impl ClipboardContent
impl ClipboardContent
Sourcepub fn bytes(&self) -> Option<&[u8]>
pub fn bytes(&self) -> Option<&[u8]>
The payload, if the read succeeded.
None for both Empty and
Unavailable — the “just give me the bytes”
accessor, for a caller that treats every non-answer the same.
Sourcepub fn text(&self) -> Option<&str>
pub fn text(&self) -> Option<&str>
The payload as text, when it is valid UTF-8.
The overwhelmingly common paste, and the one place it is worth saving
every caller the same three lines. Invalid UTF-8 answers None rather
than being lossily replaced: a paste that silently corrupts what was
copied is worse than one that refuses.