Question: Long gone are the days of creating marshmallow schemas identical to my models. I found [this excellent answer](https://stackoverflow.com/a/42892443/4097322) that explained how I could auto generate schemas from my SQA models using a simple decorator, so I implemented it and replaced the deprecated `ModelSchema` for the newer `SQLAlchemyAutoSchema`:

```
def add_schema(cls):
    class Schema(SQLAlchemyAutoSchema):
        class Meta:
            model = cls
    cls.Schema = Schema
return cls

```

This worked great... until I bumped into a model with a bloody `Enum`.

The error: `Object of type MyEnum is not JSON serializable`

I searched online and I found [this useful answer](https://stackoverflow.com/a/45026116).

But I'd like to implement it as part of the decorator so that it is generated automatically as well. In other words, I'd like to automatically overwrite all Enums in my model with `EnumField(TheEnum, by_value=True)` when generating the schema using the `add_schema` decorator; that way I won't have to overwrite all the fields manually.

What would be the best way to do this?

Answer: 
This modification of your code does not allocate any `String`s and also does not construct a new `SmallRng` each time, but I have not benchmarked it:

```
fn write_random_lines<W>(
    file: &mut W,
    line_size: usize,
    line_count: usize,
) -> Result<(), Box<dyn Error>>
where
    W: std::io::Write,
{
    // One random data iterator.
    let mut rng_iter = rand::rngs::SmallRng::from_entropy()
        .sample_iter(rand::distributions::Alphanumeric);

    // Temporary storage for encoding of chars. If the characters used
    // are not all ASCII then its size should be increased to 4.
    let mut char_buffer = [0; 1];

    for _ in 0..line_count {
        for _ in 0..line_size {
            file.write(
                rng_iter.next()
                    .unwrap()  // iterator is infinite so this never fails
                    .encode_utf8(&mut char_buffer)
                    .as_bytes())?;
        }
        file.write("\n".as_bytes())?;
    }
    Ok(())
}

```

I am new to Rust so it may be missing some ways to tidy it up. Also, note that this writes only one character at a time; if your `W` is more expensive per operation than an in-memory buffer, you probably want to wrap it in [`std::io::BufWriter`](https://doc.rust-lang.org/std/io/struct.BufWriter.html), which will batch writes to the destination (using a buffer that needs to be allocated, but only once).
I (MakotoE) benchmarked Kevin Reid's answer, and it seems their method is faster though memory allocation seems to be the same.

Benchmarking time-wise:

```
#[cfg(test)]
mod tests {
    extern crate test;
    use test::Bencher;
    use super::*;

    #[bench]
    fn bench_write_random_lines0(b: &mut Bencher) {
        let mut data: Vec<u8> = Vec::new();
        data.reserve(100 * 1000000);
        b.iter(|| {
            write_random_lines0(&mut data, 100, 1000000).unwrap();
            data.clear();
        });
    }

    #[bench]
    fn bench_write_random_lines1(b: &mut Bencher) {
        let mut data: Vec<u8> = Vec::new();
        data.reserve(100 * 1000000);
        b.iter(|| {
            // This is Kevin's implementation
            write_random_lines1(&mut data, 100, 1000000).unwrap();
            data.clear();
        });
    }
}

```

```
test tests::bench_write_random_lines0 ... bench: 764,953,658 ns/iter (+/- 7,597,989)
test tests::bench_write_random_lines1 ... bench: 360,662,595 ns/iter (+/- 886,456)

```

Benchmarking memory usage using valgrind's Massif shows that both are about the same. Mine used 3.072 Gi total, 101.0 MB at peak level. Kevin's used 4.166 Gi total, 128.0 MB peak.