From 300927f136793b39f819b4c471b52e98a1e35a1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Gonz=C3=A1lez?= Date: Thu, 21 Dec 2023 19:40:16 +0100 Subject: [PATCH] Add non-consuming `as_json` method to `Document` struct Currently, when serializing a glTF `Document`, the responsible code must own the `Document` to call the `into_json` method. However, this requirement deviates from the usual read-only borrow requirement for serializing data. To overcome this, client code can arrange for passing around references to the inner `Root` struct, but doing so sacrifices the benefits provided by the `Gltf` and `Document` structs for better binary data handling. To improve on this, let's introduce a non-consuming `as_json` unwrapping method. This method takes the `Document` by reference and returns a reference to the inner `Root` structure. By doing this, its usage across various use cases is simplified, making the code more straightforward and intuitive. --- src/lib.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 065d7bc9..33f30406 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -373,6 +373,11 @@ impl Document { self.0 } + /// Unwraps the glTF document, without consuming it. + pub fn as_json(&self) -> &json::Root { + &self.0 + } + /// Perform validation checks on loaded glTF. pub(crate) fn validate(&self) -> Result<()> { use json::validation::Validate;