Add conversion traits to Builder #12
Loading…
Reference in New Issue
No description provided.
Delete Branch ":to-string"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
This adds several traits for converting a Builder into different things, such as:
String
viaToString
,&[Node]
viaAsRef<[Node]>
&mut [Node]
viaAsMut<[Node]>
Vec<Node>
viaFrom<Builder> for Vec<Node>
Some of these changes are pretty small as far as what some users might see (e.g. you might as well call
.build()
instead of.into()
, but when writing methods that work with Nodes, it might be very nice to be able to acceptInto<Vec<Node>>
orAsRef<[Node]>
, which would allow you to take in either aBuilder
or a finishedVec<Node>
.The
ToString
method also adds some very nice functionality in that it makes rendering a document to a String much easier and readable, as compared to constructing a buffer, rendering to a mutable reference of the buffer, and converting the buffer back to aString
, all the while handling a mess of errorsMajor Changes
str::ToString
,AsRef<[Node]>
, andAsMut<[Node]>
forBuilder
AsRef<[Node]>
inrender()
(including accepting the oldVec<Node>
, so not breaking)estimate_len()
to Node, used to pre-allocate the correct size of theString
bufferFrom<Builder>
onVec<Node>
Design Decisions
estimate_len()
has some quick doctests and examples. I know most of the rest of the project uses test methods, but I hope this is alright given that the tests may add some more clarity to the purpose and function of the method.to_string()
has a single line of unsafe code. As the associated comment explains, this is provably safe, and exists just to avoid having to choose between having a bunch of duplicate code or inefficiently performing a UTF-8 check on a whole bunch bytes that we already know are safe. That said, I totally get it if you're just generally against unsafe code and will change it to be an alternative if you so wishToString
is implemented instead ofDisplay
. This is to discourage users from directly using this in aprintln!()
orwrite!()
macro, which would not be a thing you would normally expect to do with this. It also gives us the advantage of being able to pre-allocate a buffer size, meaning less expensive String resizing.render()
to work with bothio::Write
s orfmt::Write
s without duplicating the code, but I'm dumb and might be missing something, so if there's a way to do that instead of doing my funky unsafe hack that's cool and I can do that instead.Also hi! Sorry for barging in on your nice gitea instance without asking first I just kinda wanted to submit this PR. If that wasn't cool I can go tho. I'm a singlet but not like the mean kind I can be nice I promise but also I totally get it if you don't want me here sorry thanks
also uhh is that failing test my fault or is it just like that sometimes
Impl ToString for Builder, accept AsRef<[Node]> in `render()`to Add conversion traits to BuiledrAdd conversion traits to Builedrto Add conversion traits to BuilderGhost referenced this pull request2020-11-30 19:09:07 +00:00
Thank you so much! I'll fix any issues locally. CI is finnicky sometimes.
ofc! thank you!