Skip to content

write! for RichTextBuilder#1596

Merged
cmyr merged 6 commits intolinebender:masterfrom
maan2003:write-for-rich-text-builder
Feb 17, 2021
Merged

write! for RichTextBuilder#1596
cmyr merged 6 commits intolinebender:masterfrom
maan2003:write-for-rich-text-builder

Conversation

@maan2003
Copy link
Collaborator

No description provided.

@maan2003 maan2003 requested a review from cmyr February 17, 2021 18:23
Copy link
Member

@cmyr cmyr left a comment

Choose a reason for hiding this comment

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

Interesting. I trust you've found this useful for something and it's not very intrusive, so I'm happy to merge. A few little questions inline, but nothing blocking.

/// Glue for usage of the write! macro.
///
/// This method should generally not be invoked manually, but rather through the write! macro itself.
pub fn write_fmt(&mut self, fmt: std::fmt::Arguments<'_>) -> AttributesAdder {
Copy link
Member

Choose a reason for hiding this comment

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

Maybe add #[doc(hidden)] on this, since it's really an implementation detail and doesn't need to show up in public docs?

Hmm, should we be implementing the Write trait directly? Is this depending on an implementation detail of the macro, where it calls write_fmt with no qualifiers, instead of doing like Write::write_fmt(writer, args)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I didn't implement Write because it returns a Result, I wanted it to feel more like format!.

Is this depending on an implementation detail of the macro?

the docs say it just needs something with write_fmt method.

The writer may be any value with a write_fmt method; generally this comes from an implementation of either the fmt::Write or the io::Write trait.

@maan2003
Copy link
Collaborator Author

you've found this useful for something and it's not very intrusive.

I found myself doing a lot of builder.push(&format!(....)). The first thought was to make push take impl Display.
which will be make it builder.push(format_args!(...)). write! is just a cleaner interface.

@cmyr
Copy link
Member

cmyr commented Feb 17, 2021

It's possible this also saves us an intermediate allocation, although I'm not sure about this part.

@cmyr cmyr merged commit b9070eb into linebender:master Feb 17, 2021
@maan2003 maan2003 deleted the write-for-rich-text-builder branch February 18, 2021 02:09
richard-uk1 pushed a commit to richard-uk1/druid that referenced this pull request Apr 6, 2021
* write! for RichTextBuilder

* panic on formatting error

* Fix doc test

* Make write_fmt doc hidden

Co-authored-by: Colin Rofls <colin@cmyr.net>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants