From 02910ee3f585cb71f82dc258111216755047fdd9 Mon Sep 17 00:00:00 2001 From: ijl Date: Tue, 28 Feb 2023 13:29:37 +0000 Subject: [PATCH] writeext feature --- .github/workflows/linux-cross.yaml | 1 + .github/workflows/manylinux2014.yaml | 1 + .github/workflows/manylinux_2_28.yaml | 1 + build.rs | 18 +++++++++++++++--- ci/azure-macos.yml | 2 ++ src/serialize/mod.rs | 1 + src/serialize/serializer.rs | 4 ++++ 7 files changed, 25 insertions(+), 3 deletions(-) diff --git a/.github/workflows/linux-cross.yaml b/.github/workflows/linux-cross.yaml index 1403522e..539a8ae9 100644 --- a/.github/workflows/linux-cross.yaml +++ b/.github/workflows/linux-cross.yaml @@ -27,6 +27,7 @@ jobs: CFLAGS: "-O2 -fno-plt" LDFLAGS: "-O2 -flto -Wl,--as-needed" CARGO_UNSTABLE_SPARSE_REGISTRY: "true" + ORJSON_ENABLE_WRITEEXT: "1" with: maturin-version: v0.14.14 target: ${{ matrix.target.arch }} diff --git a/.github/workflows/manylinux2014.yaml b/.github/workflows/manylinux2014.yaml index 141022d8..c9f24dbb 100644 --- a/.github/workflows/manylinux2014.yaml +++ b/.github/workflows/manylinux2014.yaml @@ -19,6 +19,7 @@ jobs: CFLAGS: "-O2 -fno-plt" LDFLAGS: "-O2 -flto -Wl,--as-needed" CARGO_UNSTABLE_SPARSE_REGISTRY: "true" + ORJSON_ENABLE_WRITEEXT: "1" container: image: quay.io/pypa/manylinux2014_x86_64:latest options: --user 0 diff --git a/.github/workflows/manylinux_2_28.yaml b/.github/workflows/manylinux_2_28.yaml index d263c006..d73378a3 100644 --- a/.github/workflows/manylinux_2_28.yaml +++ b/.github/workflows/manylinux_2_28.yaml @@ -20,6 +20,7 @@ jobs: LDFLAGS: "-O2 -flto=thin -fuse-ld=lld -Wl,--as-needed" RUSTFLAGS: "-C linker=clang -C link-arg=-fuse-ld=lld" CARGO_UNSTABLE_SPARSE_REGISTRY: "true" + ORJSON_ENABLE_WRITEEXT: "1" container: image: quay.io/pypa/manylinux_2_28_x86_64:latest options: --user 0 diff --git a/build.rs b/build.rs index 39443a23..7fcc1a5f 100644 --- a/build.rs +++ b/build.rs @@ -1,5 +1,7 @@ // SPDX-License-Identifier: (Apache-2.0 OR MIT) +use std::env; + fn main() { println!("cargo:rerun-if-changed=build.rs"); println!("cargo:rerun-if-changed=include/yyjson/*"); @@ -7,7 +9,9 @@ fn main() { println!("cargo:rerun-if-env-changed=CFLAGS"); println!("cargo:rerun-if-env-changed=LDFLAGS"); println!("cargo:rerun-if-env-changed=RUSTFLAGS"); + println!("cargo:rerun-if-env-changed=ORJSON_DISABLE_WRITEEXT"); println!("cargo:rerun-if-env-changed=ORJSON_DISABLE_YYJSON"); + println!("cargo:rerun-if-env-changed=ORJSON_ENABLE_WRITEEXT"); let py_cfg = pyo3_build_config::get(); py_cfg.emit_pyo3_cfgs(); @@ -20,8 +24,16 @@ fn main() { println!("cargo:rustc-cfg=feature=\"optimize\""); } - if std::env::var("ORJSON_DISABLE_YYJSON").is_ok() { - if std::env::var("CARGO_FEATURE_YYJSON").is_ok() { + if env::var("ORJSON_DISABLE_WRITEEXT").is_ok() { + } else if env::var("ORJSON_ENABLE_WRITEEXT").is_ok() + || env::var("CARGO_CFG_TARGET_OS").unwrap() == "macos" + || env::var("CARGO_CFG_TARGET_ENV").unwrap() == "gnu" + { + println!("cargo:rustc-cfg=feature=\"writeext\""); + } + + if env::var("ORJSON_DISABLE_YYJSON").is_ok() { + if env::var("CARGO_FEATURE_YYJSON").is_ok() { panic!("ORJSON_DISABLE_YYJSON and --features=yyjson both enabled.") } } else { @@ -36,7 +48,7 @@ fn main() { println!("cargo:rustc-cfg=feature=\"yyjson\""); } Err(_) => { - if std::env::var("CARGO_FEATURE_YYJSON").is_ok() { + if env::var("CARGO_FEATURE_YYJSON").is_ok() { panic!("yyjson was enabled but the build failed. To build with a different backend do not specify the feature.") } } diff --git a/ci/azure-macos.yml b/ci/azure-macos.yml index b4e7abb6..f2bd8b9d 100644 --- a/ci/azure-macos.yml +++ b/ci/azure-macos.yml @@ -20,6 +20,7 @@ steps: LDFLAGS: "-O2 -flto=thin -fuse-ld=lld -Wl,--as-needed" RUSTFLAGS: "-C linker=clang -C target-feature=+sse4.2" CARGO_UNSTABLE_SPARSE_REGISTRY: "true" + ORJSON_ENABLE_WRITEEXT: "1" displayName: build - bash: pip install target/wheels/orjson*.whl displayName: install @@ -37,6 +38,7 @@ steps: LDFLAGS: "-O2 -flto=thin -fuse-ld=lld -Wl,--as-needed" RUSTFLAGS: "-C linker=clang" CARGO_UNSTABLE_SPARSE_REGISTRY: "true" + ORJSON_ENABLE_WRITEEXT: "1" displayName: build universal2 - bash: pip install --force-reinstall target/wheels/orjson*universal2.whl displayName: install universal2 diff --git a/src/serialize/mod.rs b/src/serialize/mod.rs index 06412237..2f58a339 100644 --- a/src/serialize/mod.rs +++ b/src/serialize/mod.rs @@ -18,6 +18,7 @@ mod tuple; mod uuid; mod writer; +#[cfg(feature = "writeext")] mod json; pub use serializer::serialize; diff --git a/src/serialize/serializer.rs b/src/serialize/serializer.rs index f6ebd6f6..05ad2eca 100644 --- a/src/serialize/serializer.rs +++ b/src/serialize/serializer.rs @@ -21,6 +21,10 @@ use serde::ser::{Serialize, SerializeMap, Serializer}; use std::io::Write; use std::ptr::NonNull; +#[cfg(not(feature = "writeext"))] +use serde_json::{to_writer, to_writer_pretty}; + +#[cfg(feature = "writeext")] use crate::serialize::json::{to_writer, to_writer_pretty}; pub const RECURSION_LIMIT: u8 = 255;