Repository: tokio-rs/axum
File: axum/src/response/sse.rs (Rust)

Code before:
            flags: Some(&mut self.flags),
        }
    }

    /// Set the event's data data field(s) (`data: <content>`)
    ///
    /// Newlines in `data` will automatically be broken across `data: ` fields.
    ///
    /// This corresponds to [`MessageEvent`'s data field].
    ///
    /// Note that events with an empty data field will be ignored by the browser.
    ///
    /// # Panics
    ///
    /// Panics if any `data` has already been written before.
    ///
    /// [`MessageEvent`'s data field]: https://developer.mozilla.org/en-US/docs/Web/API/MessageEvent/data
    pub fn data<T>(mut self, data: T) -> Self
    where
        T: AsRef<str>,
    {
        {
            use std::io::Write;

            let mut write = self.data_writer();
            let _ = write!(write, "{}", data.as_ref());
        }

        self
    }

    /// Set the event's data field to a value serialized as unformatted JSON (`data: <content>`).
    ///
    /// This corresponds to [`MessageEvent`'s data field].
    ///
    /// # Panics
    ///
    /// Panics if any `data` has already been written before.
    ///
    /// [`MessageEvent`'s data field]: https://developer.mozilla.org/en-US/docs/Web/API/MessageEvent/data
    #[cfg(feature = "json")]
    pub fn json_data<T>(mut self, data: T) -> Result<Self, axum_core::Error>
    where
        T: serde::Serialize,
    {
        serde_json::to_writer(self.data_writer(), &data).map_err(axum_core::Error::new)?;
        Ok(self)
    }

    /// Set the event's comment field (`:<comment-text>`).
    ///

Review comment (suggestion):
It would be more performant to use:

```rs
writer.write_all(data.as_ref().as_bytes());
```

Code after:
    /// - [`Self::json_data`] can be used as a shortcut to write `json` data
    ///
    /// Turn it into an [`Event`] again using [`EventDataWriter::into_event`].
    pub fn into_data_writer(self) -> EventDataWriter {
        EventDataWriter {
            event: self,
            data_written: false,
        }
    }

    /// Set the event's data data field(s) (`data: <content>`)
    ///
    /// Newlines in `data` will automatically be broken across `data: ` fields.
    ///
    /// This corresponds to [`MessageEvent`'s data field].
    ///
    /// Note that events with an empty data field will be ignored by the browser.
    ///
    /// # Panics
    ///
    /// Panics if any `data` has already been written before.
    ///
    /// [`MessageEvent`'s data field]: https://developer.mozilla.org/en-US/docs/Web/API/MessageEvent/data
    pub fn data<T>(self, data: T) -> Self
    where
        T: AsRef<str>,
    {
        let mut writer = self.into_data_writer();
        let _ = writer.write_str(data.as_ref());
        writer.into_event()
    }

    /// Set the event's data field to a value serialized as unformatted JSON (`data: <content>`).
    ///
    /// This corresponds to [`MessageEvent`'s data field].
    ///
    /// # Panics
    ///
    /// Panics if any `data` has already been written before.
    ///
    /// [`MessageEvent`'s data field]: https://developer.mozilla.org/en-US/docs/Web/API/MessageEvent/data
    #[cfg(feature = "json")]
    pub fn json_data<T>(self, data: T) -> Result<Self, axum_core::Error>
    where
        T: serde::Serialize,
    {
        struct JsonWriter<'a>(&'a mut EventDataWriter);
        impl std::io::Write for JsonWriter<'_> {
            #[inline]
            fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
                Ok(self.0.write_buf(buf))