Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Writeable WithPart helper #5328

Merged
merged 1 commit into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions utils/writeable/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ pub mod adapters {
use super::*;

pub use parts_write_adapter::CoreWriteAsPartsWrite;
pub use parts_write_adapter::WithPart;
pub use try_writeable::TryWriteableInfallibleAsWriteable;
pub use try_writeable::WriteableAsTryWriteableInfallible;
}
Expand Down
86 changes: 86 additions & 0 deletions utils/writeable/src/parts_write_adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use crate::*;

/// A wrapper around a type implementing [`fmt::Write`] that implements [`PartsWrite`].
#[derive(Debug)]
#[allow(clippy::exhaustive_structs)] // newtype
pub struct CoreWriteAsPartsWrite<W: fmt::Write + ?Sized>(pub W);
Expand Down Expand Up @@ -32,3 +33,88 @@ impl<W: fmt::Write + ?Sized> PartsWrite for CoreWriteAsPartsWrite<W> {
f(self)
}
}

/// A [`Writeable`] that writes out the given part.
///
/// # Examples
///
/// ```
/// use writeable::Part;
/// use writeable::assert_writeable_parts_eq;
/// use writeable::adapters::WithPart;
///
/// // Simple usage:
///
/// const PART: Part = Part {
/// category: "foo",
/// value: "bar",
/// };
///
/// assert_writeable_parts_eq!(
/// WithPart {
/// writeable: "Hello World",
/// part: PART
/// },
/// "Hello World",
/// [(0, 11, PART)],
/// );
///
/// // Can be nested:
///
/// const PART2: Part = Part {
/// category: "foo2",
/// value: "bar2",
/// };
///
/// assert_writeable_parts_eq!(
/// WithPart {
/// writeable: WithPart {
/// writeable: "Hello World",
/// part: PART
/// },
/// part: PART2
/// },
/// "Hello World",
/// [(0, 11, PART), (0, 11, PART2)],
/// );
/// ```
#[derive(Debug)]
#[allow(clippy::exhaustive_structs)] // public adapter
pub struct WithPart<T: ?Sized> {
pub part: Part,
pub writeable: T,
}

impl<T: Writeable + ?Sized> Writeable for WithPart<T> {
#[inline]
fn write_to<W: fmt::Write + ?Sized>(&self, sink: &mut W) -> fmt::Result {
self.writeable.write_to(sink)
}

#[inline]
fn write_to_parts<W: PartsWrite + ?Sized>(&self, sink: &mut W) -> fmt::Result {
sink.with_part(self.part, |w| self.writeable.write_to_parts(w))
}

#[inline]
fn writeable_length_hint(&self) -> LengthHint {
self.writeable.writeable_length_hint()
}

#[inline]
fn write_to_string(&self) -> Cow<str> {
self.writeable.write_to_string()
}

#[inline]
fn writeable_cmp_bytes(&self, other: &[u8]) -> core::cmp::Ordering {
self.writeable.writeable_cmp_bytes(other)
}
}

impl<T: Writeable + ?Sized> fmt::Display for WithPart<T> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use the macro to implement this?

Copy link
Member Author

@sffc sffc Aug 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The macro doesn't support <T>. I've been wanting to maybe fix that, but it's not clear what would be the best syntax.

#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Writeable::write_to(&self, f)
}
}