Skip to content

Conversation

@Thomas-Mewily
Copy link
Contributor

Hello, this is my first pull request :)
I will try to do my best, don’t hesitate to give me any tips on how I can improve.

Description

This add an into_inner() method to ron::ser::Serializer, allowing users to get back the inner writer.

The name and documentation is the same as the serde_json::Serializer::into_inner().
(it also have the MIT OR Apache-2.0 licence)

Motivation

A typical use case of ron looks like:

  // serializer part : serializer + buffer
  let mut buf = String::new();
  let mut ron_serializer = ron::ser::Serializer::new(&mut buf, None).unwrap();
  
  // The operation that give back the ron representation in buf
  "some structure".serialize(&mut ron_serializer).unwrap();
  println!("{buf}")

However, wrapping this operation into a trait + a 'static structure is impossible without an into_inner():

pub trait MarkupSerializer: Serializer {
    fn extract_markup(self) -> String; // Should be a result, but anyway this is an example
}
impl MarkupSerializer for &mut ron::ser::Serializer<String>
{
    fn extract_markup(self) -> String {
        panic!("impossible without an into_inner")
    }
}

The only way is to also store the buf in the same structure, but that can't be express in rust to my knowledge:

pub struct StringRonSerializer {
    buf: String,
    serializer: ron::Serializer<&'self mut String>, // Imaginary syntax. Reference to Self::buf . Don't compile/Not possible
}

So I don't think storing a ron serializer to progressively serialize somethings, them getting back the serialized string in a lifetimeless / 'static structure is not possible without an into_inner() method.

(My use case is to wrap a ron serializer into another serializer)

@juntyr
Copy link
Member

juntyr commented Oct 24, 2025

Looks good to me, can you also add a changelog entry?

@Thomas-Mewily
Copy link
Contributor Author

I added an entry to the changelog, although I'm not sure I put it in the right place.

@juntyr
Copy link
Member

juntyr commented Oct 25, 2025

The test failures will be fixed in #582

@juntyr juntyr merged commit 9b9b88e into ron-rs:master Oct 25, 2025
2 of 12 checks passed
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