Skip to main content

ClipboardContent

Enum ClipboardContent 

Source
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. Reporting None there 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.

§

Unavailable

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

Source

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.

Source

pub fn into_bytes(self) -> Option<Vec<u8>>

The payload, consumed. See bytes.

Source

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.

Source

pub const fn name(&self) -> &'static str

A short, stable name for logs and test assertions.

Trait Implementations§

Source§

impl Clone for ClipboardContent

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ClipboardContent

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for ClipboardContent

Source§

impl PartialEq for ClipboardContent

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for ClipboardContent

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.