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: 
You can easily reuse a `String` in a loop by creating it outside the loop and `clear`ing it after using the contents:

```
    // Use Kevin's suggestion not to make a new `SmallRng` each time:
    let mut rng_iter =
        rand::rngs::SmallRng::from_entropy().sample_iter(rand::distributions::Alphanumeric);
    let mut s = String::with_capacity(line_size + 1);  // allocate the buffer
    for _ in 0..line_count {
        s.extend(rng_iter.by_ref().take(line_size));   // fill the buffer
        s.push('\n');
        file.write(s.as_bytes())?;                     // use the contents
        s.clear();                                     // clear the buffer
    }

```

`String::clear` erases the contents of the `String` (dropping if necessary), but does not free its backing buffer, so it can be reused without needing to reallocate.

See also
--------

* [Weird behaviour when using read\_line in a loop](https://stackoverflow.com/q/45232943/3650362)
* [Why does Iterator::take\_while take ownership of the iterator?](https://stackoverflow.com/q/31374051/3650362) explains why `by_ref` is needed
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).