mirror of
https://github.com/Noratrieb/mono-fmt.git
synced 2026-01-14 15:25:08 +01:00
more impls
This commit is contained in:
parent
8862186a1f
commit
98025d204f
4 changed files with 72 additions and 49 deletions
25
src/args.rs
25
src/args.rs
|
|
@ -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),*};
|
||||
}
|
||||
|
|
|
|||
22
src/lib.rs
22
src/lib.rs
|
|
@ -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};
|
||||
|
||||
|
|
|
|||
|
|
@ -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('"')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
36
src/write.rs
36
src/write.rs
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue