use super::*;
/// Builder for sending a message to a remote peer.
///
/// This builder can be constructed via [`Stream::message`].
#[derive(Clone, Copy)]
pub struct MessageBuilder<Message, Sender> {
    pub(crate) sender: Sender,
    pub(crate) message: Message,
}

impl<Payload: IntoPayload> MessageBuilder<Payload, &'_ datastreams_ffi::Stream> {
    /// Convert the message into a [`Bytes`] buffer and send it.
    ///
    /// # Errors
    /// If conversion failed.
    pub fn try_send(self) -> Result<SendHandle, Payload::Error> {
        Ok(SendHandle {
            handle: self.sender.inner.send(self.message.into_payload()?),
        })
    }

    /// Convert the message into a [`Bytes`] buffer infallibly and send it.
    pub fn send(self) -> SendHandle
    where
        Payload: IntoPayload<Error = core::convert::Infallible>,
    {
        self.try_send().unwrap()
    }
}
