Skip to content

Commit

Permalink
Add impl<T> Writeable for [T]
Browse files Browse the repository at this point in the history
  • Loading branch information
sffc committed Apr 10, 2024
1 parent bfee812 commit f8f4b89
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
40 changes: 40 additions & 0 deletions utils/writeable/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,46 @@ impl_write_smart_pointer!(alloc::boxed::Box<T>);
impl_write_smart_pointer!(alloc::rc::Rc<T>);
impl_write_smart_pointer!(alloc::sync::Arc<T>);

/// Concatenates the elements of a slice.
///
/// # Examples
///
/// ```
/// use writeable::Writeable;
/// use writeable::assert_writeable_eq;
///
/// assert_writeable_eq!(
/// @skip display,
/// &["Hello", ", ", "world", "!"][..],
/// "Hello, world!"
/// );
/// ```
impl<T> Writeable for [T]
where
T: Writeable,
{
#[inline]
fn write_to<W: fmt::Write + ?Sized>(&self, sink: &mut W) -> fmt::Result {
self.iter().try_for_each(|t| t.write_to(sink))
}

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

#[inline]
fn writeable_length_hint(&self) -> LengthHint {
self.iter().map(Writeable::writeable_length_hint).sum()
}

// Use default write_to_string impl since it doesn't make sense to forward the inner impls.
// Note: We could optimize in the case of 1 element in the list.

// Also use default write_cmp_bytes impl. Forwarding would require knowing how many bytes
// each writeable could consume, information we don't necesarily have.
}

#[test]
fn test_string_impls() {
fn check_writeable_slice<W: Writeable + core::fmt::Display>(writeables: &[W]) {
Expand Down
9 changes: 8 additions & 1 deletion utils/writeable/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,14 @@ macro_rules! assert_writeable_eq {
$crate::assert_writeable_eq!($actual_writeable, $expected_str, "");
};
($actual_writeable:expr, $expected_str:expr, $($arg:tt)+) => {{
let actual_writeable = &$actual_writeable;
$crate::assert_writeable_eq!(@skip display, $actual_writeable, $expected_str, $($arg)+);
assert_eq!(actual_writeable.to_string(), $expected_str);
}};
(@skip display, $actual_writeable:expr, $expected_str:expr $(,)?) => {
$crate::assert_writeable_eq!(@skip display, $actual_writeable, $expected_str, "");
};
(@skip display, $actual_writeable:expr, $expected_str:expr, $($arg:tt)+) => {{
let actual_writeable = &$actual_writeable;
let (actual_str, _) = $crate::_internal::writeable_to_parts_for_test(actual_writeable);
assert_eq!(actual_str, $expected_str, $($arg)*);
Expand All @@ -420,7 +428,6 @@ macro_rules! assert_writeable_eq {
length_hint.0, actual_str.len(), format!($($arg)*),
);
}
assert_eq!(actual_writeable.to_string(), $expected_str);
let ordering = $crate::Writeable::write_cmp_bytes(actual_writeable, $expected_str.as_bytes());
assert_eq!(ordering, core::cmp::Ordering::Equal, $($arg)*);
}};
Expand Down

0 comments on commit f8f4b89

Please sign in to comment.