more impls

This commit is contained in:
nora 2022-09-24 22:20:53 +02:00
parent 8862186a1f
commit 98025d204f
4 changed files with 72 additions and 49 deletions

View file

@ -48,6 +48,13 @@ impl Arguments for Str {
}
}
macro_rules! not_for_pointer {
(Pointer $($tt:tt)*) => {};
($_not_pointer:ident $($tt:tt)*) => {
$($tt)*
}
}
macro_rules! traits {
($(struct $name:ident: trait $trait:ident);* $(;)?) => {
$(
@ -66,6 +73,24 @@ macro_rules! traits {
}
)*
$(
not_for_pointer! {
$trait
impl<T: $trait + ?Sized> $trait for &T {
fn fmt<W: Write, O: FmtOpts>(&self, f: &mut Formatter<W, O>) -> Result {
<T as $trait>::fmt(&self, f)
}
}
impl<T: $trait + ?Sized> $trait for &mut T {
fn fmt<W: Write, O: FmtOpts>(&self, f: &mut Formatter<W, O>) -> Result {
<T as $trait>::fmt(&self, f)
}
}
}
)*
pub mod macro_exports {
pub use super::{$($name, $trait),*};
}

View file

@ -33,28 +33,6 @@ pub trait Write {
}
}
impl Write for String {
fn write_str(&mut self, str: &str) -> Result {
self.push_str(str);
Ok(())
}
fn write_char(&mut self, char: char) -> Result {
self.push(char);
Ok(())
}
}
impl<W: Write> Write for &mut W {
fn write_str(&mut self, str: &str) -> Result {
<W as Write>::write_str(self, str)
}
fn write_char(&mut self, char: char) -> Result {
<W as Write>::write_char(self, char)
}
}
pub mod helpers {
use crate::{Arguments, Formatter, Result, Write};

View file

@ -6,12 +6,6 @@ mod impl_prelude {
pub use crate::*;
}
impl<T: Debug + ?Sized> Debug for &T {
fn fmt<W: Write, O: FmtOpts>(&self, f: &mut Formatter<W, O>) -> Result {
<T as Debug>::fmt(&self, f)
}
}
impl<T: Debug, const N: usize> Debug for [T; N] {
fn fmt<W: Write, O: FmtOpts>(&self, f: &mut Formatter<W, O>) -> Result {
<[T] as Debug>::fmt(&&self[..], f)
@ -96,3 +90,35 @@ mod char {
}
}
}
mod strings {
use super::impl_prelude::*;
impl Display for String {
fn fmt<W: Write, O: FmtOpts>(&self, f: &mut Formatter<W, O>) -> Result {
f.write_str(self)
}
}
impl Display for str {
fn fmt<W: Write, O: FmtOpts>(&self, f: &mut Formatter<W, O>) -> Result {
f.write_str(self)
}
}
impl Debug for str {
fn fmt<W: Write, O: FmtOpts>(&self, f: &mut Formatter<W, O>) -> Result {
f.write_char('"')?;
f.write_str(self)?;
f.write_char('"')
}
}
impl Debug for String {
fn fmt<W: Write, O: FmtOpts>(&self, f: &mut Formatter<W, O>) -> Result {
f.write_char('"')?;
f.write_str(self)?;
f.write_char('"')
}
}
}

View file

@ -1,29 +1,23 @@
use crate::{Debug, Display, FmtOpts, Formatter, Result, Write};
use crate::{Result, Write};
impl Debug for &'_ str {
fn fmt<W: Write, O: FmtOpts>(&self, f: &mut Formatter<W, O>) -> Result {
f.write_char('"')?;
f.write_str(self)?;
f.write_char('"')
impl Write for String {
fn write_str(&mut self, str: &str) -> Result {
self.push_str(str);
Ok(())
}
fn write_char(&mut self, char: char) -> Result {
self.push(char);
Ok(())
}
}
impl Display for &'_ str {
fn fmt<W: Write, O: FmtOpts>(&self, f: &mut Formatter<W, O>) -> Result {
f.write_str(self)
impl<W: Write> Write for &mut W {
fn write_str(&mut self, str: &str) -> Result {
<W as Write>::write_str(self, str)
}
}
impl Debug for String {
fn fmt<W: Write, O: FmtOpts>(&self, f: &mut Formatter<W, O>) -> Result {
f.write_char('"')?;
f.write_str(self)?;
f.write_char('"')
}
}
impl Display for String {
fn fmt<W: Write, O: FmtOpts>(&self, f: &mut Formatter<W, O>) -> Result {
f.write_str(self)
fn write_char(&mut self, char: char) -> Result {
<W as Write>::write_char(self, char)
}
}