# Stack allocated BufWriter (and maybe BufReader)

**URL:** <https://internals.rust-lang.org/t/stack-allocated-bufwriter-and-maybe-bufreader/15878>\
**Category:** libs\
**Created:** [December 29, 2021, 7:04pm UTC](https://internals.rust-lang.org/t/stack-allocated-bufwriter-and-maybe-bufreader/15878 "2021-12-29T19:04:39Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![SUPERCILEX](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/supercilex/32/8541_2.png) [@SUPERCILEX](https://internals.rust-lang.org/u/SUPERCILEX)\
**Post date:** [December 29, 2021, 7:04pm UTC](https://internals.rust-lang.org/t/stack-allocated-bufwriter-and-maybe-bufreader/15878/1 "2021-12-29T19:04:39Z")

</div>

Is there a crate or std API that has a similar API to `Buf{Writer,Reader}` and doesn't perform any heap allocations? Since const generics are stable, shouldn't it be possible to implement both the writer and reader using only a stack allocated buffer? I couldn't find a crate for this and was wondering why it isn't part of the stdlib as something like `StackBuf{Writer,Reader}`. Any reason it shouldn't be part of the stdlib?

---

<div class="post-metadata">

**Author:** ![hyeonu](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/hyeonu/32/2162_2.png) [@hyeonu](https://internals.rust-lang.org/u/hyeonu)\
**Post date:** [December 30, 2021, 1:05am UTC](https://internals.rust-lang.org/t/stack-allocated-bufwriter-and-maybe-bufreader/15878/2 "2021-12-30T01:05:29Z")

</div>

> [@SUPERCILEX](#):
>
> Any reason it shouldn't be part of the stdlib?

You don't need to find a reason it shouldn't be part of the stdlib. You need to find a reason it should be to convince community. Easiest way to prove it is to implement it as a standalone crate published on the [crates.io](http://crates.io) and see how many people find it useful.

Adding something to the stdlib is not a trivial task since once something is included we should maintain it for decades. For example the python stdlib has urllib, urllib2, and urllib3 while the recommended library from the community is the reqwest from pip(python's [crates.io](http://crates.io)). But the python core team need to keep maintaining all 3 of them since otherwise it would be the language's breaking change.

---

<div class="post-metadata">

**Author:** ![SUPERCILEX](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/supercilex/32/8541_2.png) [@SUPERCILEX](https://internals.rust-lang.org/u/SUPERCILEX)\
**Post date:** [December 30, 2021, 8:14pm UTC](https://internals.rust-lang.org/t/stack-allocated-bufwriter-and-maybe-bufreader/15878/3 "2021-12-30T20:14:34Z")

</div>

Good point. I'm wondering if that actually works though? I.e. are there examples of code from crates making it into the stdlib? I guess I'm not clear on what would make a crate stdlib worthy.

---

<div class="post-metadata">

**Author:** ![Lonami](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/lonami/32/6774_2.png) [@Lonami](https://internals.rust-lang.org/u/Lonami)\
**Post date:** [December 30, 2021, 8:58pm UTC](https://internals.rust-lang.org/t/stack-allocated-bufwriter-and-maybe-bufreader/15878/4 "2021-12-30T20:58:26Z")

</div>

[`once_cell`](https://github.com/rust-lang/rust/issues/74465) is one such example of a popular crate / feature making its way into the standard library, as well as how the core traits of futures have made it in.

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/steffahn/32/13288_2.png) [@steffahn](https://internals.rust-lang.org/u/steffahn)\
**Post date:** [December 30, 2021, 10:03pm UTC](https://internals.rust-lang.org/t/stack-allocated-bufwriter-and-maybe-bufreader/15878/5 "2021-12-30T22:03:05Z")

</div>

Well, at least for the `Future` trait itself the motivation is different, because it _needs_ to be in the standard library in order for `async fn` to be able to implement it. But once\_cell is a good example.

---

<div class="post-metadata">

**Author:** ![SUPERCILEX](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/supercilex/32/8541_2.png) [@SUPERCILEX](https://internals.rust-lang.org/u/SUPERCILEX)\
**Post date:** [January 4, 2022, 9:36pm UTC](https://internals.rust-lang.org/t/stack-allocated-bufwriter-and-maybe-bufreader/15878/6 "2022-01-04T21:36:43Z")

</div>

Thanks for the feedback and examples everyone! It turns out the stdlib implementations never realloc, so I was able to mostly copy them veratem. Here's the crate I created: [https://crates.io/crates/stack-buffer](https://crates.io/crates/stack-buffer).

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [April 4, 2022, 9:37pm UTC](https://internals.rust-lang.org/t/stack-allocated-bufwriter-and-maybe-bufreader/15878/7 "2022-04-04T21:37:17Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
