Skip to main content

ps_ecc/cow/methods/
try_into_buffer.rs

1use ps_buffer::{Buffer, BufferError, SharedBuffer};
2
3use crate::Cow;
4
5impl Cow<'_> {
6    /// Converts this [`Cow`] into a [`SharedBuffer`].
7    /// - In the case of [`Cow::Borrowed`], a new buffer is allocated.
8    /// - In the case of [`Cow::Owned`], the existing buffer is returned.
9    /// # Errors
10    /// [`BufferError`] is returned if an allocation error occurs.
11    pub fn try_into_buffer(self) -> Result<SharedBuffer, BufferError> {
12        match self {
13            Cow::Borrowed(value) => Ok(Buffer::from_slice(value)?.share()),
14            Cow::Owned(value) => Ok(value),
15        }
16    }
17}