Skip to content

Commit

Permalink
feat: Introduce 'sync' and 'async' features
Browse files Browse the repository at this point in the history
  • Loading branch information
vrurg committed Dec 5, 2024
1 parent 2879b2c commit a71b871
Show file tree
Hide file tree
Showing 382 changed files with 5,460 additions and 255 deletions.
26 changes: 23 additions & 3 deletions .github/workflows/fieldx.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,30 @@ jobs:
- name: Show environment variables
run: env
- run: rustup update ${{ matrix.toolchain }} && rustup default ${{ matrix.toolchain }}
- run: cargo build --verbose --all --features ${{ matrix.feature }}
- run: cargo test --verbose --all --features ${{ matrix.feature }}
- run: cargo build --verbose --all --features sync,async,${{ matrix.feature }}
- run: cargo test --verbose --all --features sync,async,${{ matrix.feature }}
- run: cargo test --verbose --all --features serde,send_guard
if: matrix.feature == 'serde'
all_features:
runs-on: ${{ matrix.os }}-latest
strategy:
matrix:
os: [ubuntu, macos, windows]
toolchain:
- 1.77
- stable
- beta
- nightly
env:
__FIELDX_DEFAULT_TOOLCHAIN__: ${{ matrix.toolchain }}
steps:
- uses: actions/checkout@v4
- name: Show environment variables
run: env
- run: rustup update ${{ matrix.toolchain }} && rustup default ${{ matrix.toolchain }}
- run: cargo build --verbose --all --features sync,async,serde,send_guard
- run: cargo test --verbose --all --features sync,async,serde,send_guard
- run: cargo test --verbose --all --features sync,async,serde
no_features:
runs-on: ${{ matrix.os }}-latest
strategy:
Expand All @@ -54,7 +74,7 @@ jobs:
- run: rustup update ${{ matrix.toolchain }} && rustup default ${{ matrix.toolchain }}
- run: cargo build --verbose --all
- name: Test all
env:
env:
__FIELDX_DEFAULT_TOOLCHAIN__: ${{ matrix.toolchain }}
run: cargo test --verbose --all
build_docs:
Expand Down
9 changes: 5 additions & 4 deletions fieldx/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
async-trait = { workspace = true }
async-trait = { workspace = true, optional = true }
fieldx_aux = { workspace = true }
fieldx_derive = { workspace = true, features = ["tracing"] }
getset = { workspace = true }
once_cell = { workspace = true }
parking_lot = { workspace = true }
parking_lot = { workspace = true, optional = true }
serde = { workspace = true, optional = true }
thiserror = { workspace = true }
tokio = { workspace = true, features = ["sync"] }
tokio = { workspace = true, features = ["sync"], optional = true }

[dev-dependencies]
cargo-toolchain = { workspace = true }
Expand All @@ -36,9 +35,11 @@
trybuild = { workspace = true }

[features]
async = ["fieldx_derive/async", "dep:tokio", "dep:async-trait"]
diagnostics = ["fieldx_derive/diagnostics"]
send_guard = ["parking_lot/send_guard"]
serde = ["fieldx_derive/serde", "parking_lot/serde", "dep:serde"]
sync = ["fieldx_derive/sync", "dep:parking_lot"]

[package.metadata.docs.rs]
all-features = true
19 changes: 15 additions & 4 deletions fieldx/Makefile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,20 @@
category = "Test"
description = "Test code compilability"
script = '''
!include_files ../makefile.ds
echo Testing compilability with toolchain '${__FIELDX_DEFAULT_TOOLCHAIN__}'
cm_run_task compile-default-features
cm_run_task compile-serde-enabled
features = array sync async serde
feature_sets = generate_power_set ${features}
for fs in ${feature_sets}
fs = array_join ${fs} ","
echo "Testing feature set '${fs}'"
set_env __FIELDX_FEATURE_SET__ ${fs}
cm_run_task compile-with-features
end
'''
script_runner = "@duckscript"
toolchain = "${__FIELDX_DEFAULT_TOOLCHAIN__}"
Expand All @@ -16,9 +27,9 @@ cm_run_task compile-serde-enabled
description = "Test code compilability with default feature set"
toolchain = "${__FIELDX_DEFAULT_TOOLCHAIN__}"

[tasks.compile-serde-enabled]
args = ["test", "--test", "compilation", "--features", "serde"]
[tasks.compile-with-features]
args = ["test", "--test", "compilation", "--features", "${__FIELDX_FEATURE_SET__}"]
category = "Test"
command = "cargo"
description = "Test code compilability with 'serde' feature"
description = "Test code compilability with '${__FIELDX_FEATURE_SET__}' features"
toolchain = "${__FIELDX_DEFAULT_TOOLCHAIN__}"
4 changes: 4 additions & 0 deletions fieldx/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,8 +468,10 @@
//! [`parking_lot`]: https://docs.rs/parking_lot
//! [`serde`]: https://docs.rs/serde
#[cfg(feature = "async")]
pub mod r#async;
pub mod error;
#[cfg(feature = "sync")]
pub mod sync;
pub mod traits;

Expand All @@ -478,11 +480,13 @@ pub use fieldx_aux::FXOrig;
pub use fieldx_derive::fxstruct;
#[doc(hidden)]
pub use once_cell::unsync::OnceCell;
#[cfg(feature = "async")]
pub use r#async::FXProxyAsync;
#[doc(hidden)]
pub use std::{
cell::{Ref, RefCell, RefMut},
fmt,
sync::atomic::Ordering,
};
#[cfg(feature = "sync")]
pub use sync::FXProxySync;
1 change: 1 addition & 0 deletions fieldx/tests/async.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![cfg(feature = "async")]
use core::time;
use fieldx::fxstruct;
use num_cpus;
Expand Down
1 change: 1 addition & 0 deletions fieldx/tests/async_fallible.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![cfg(feature = "async")]
use fieldx::fxstruct;
use std::sync::Arc;

Expand Down
1 change: 1 addition & 0 deletions fieldx/tests/async_lock.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![cfg(feature = "async")]
use fieldx::r#async::FXRwLockAsync;
use tokio;

Expand Down
2 changes: 2 additions & 0 deletions fieldx/tests/builder_opt_in.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ struct FooPlain {
unbuildable: String,
}

#[cfg(feature = "sync")]
#[fxstruct(sync, builder(opt_in, into), default(off), get)]
#[derive(Debug)]
struct FooSync {
Expand All @@ -27,6 +28,7 @@ fn plain() {
assert_eq!(foo.unbuildable(), "explicit");
}

#[cfg(feature = "sync")]
#[test]
fn nsync() {
let foo = FooSync::builder().buildable("from builder").build().unwrap();
Expand Down
2 changes: 2 additions & 0 deletions fieldx/tests/builder_required.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ struct FooPlain {
v: i32,
}

#[cfg(feature = "sync")]
#[fxstruct(sync, builder)]
#[derive(Debug)]
struct FooSync {
Expand All @@ -22,6 +23,7 @@ fn plain() {
println!("plain: {:?}", foo);
}

#[cfg(feature = "sync")]
#[test]
fn sync() {
let foo = FooSync::builder().build();
Expand Down
25 changes: 15 additions & 10 deletions fieldx/tests/compilable/no_reader.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
// This is an "answer" to ../uncompilable/unused_reader.rs. It compiles because of reader(off)
use fieldx::fxstruct;
#[cfg(feature = "sync")]
mod inner {
// This is an "answer" to ../uncompilable/unused_reader.rs. It compiles because of reader(off)
use fieldx::fxstruct;

#[fxstruct(sync)]
struct TT {
#[fieldx(lazy, reader(off, attributes_fn(deny(dead_code))))]
tt: String,
}
#[fxstruct(sync)]
struct TT {
#[fieldx(lazy, reader(off, attributes_fn(deny(dead_code))))]
tt: String,
}

impl TT {
fn build_tt(&self) -> String { "whatever".into() }
impl TT {
fn build_tt(&self) -> String {
"whatever".into()
}
}
}

fn main() {}
fn main() {}
87 changes: 59 additions & 28 deletions fieldx/tests/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,24 @@ use trybuild;

struct UncompEnv {
// .0 is a path of .stderr under the version subdir, .1 is the one used for testing
stderrs: Vec<(PathBuf, PathBuf)>,
base_dir: String,
stderrs: Vec<(PathBuf, PathBuf)>,
base_dir: PathBuf,
outputs_dir: PathBuf,
}

impl UncompEnv {
fn new(subdir: &str) -> Self {
let mut stderrs: Vec<(PathBuf, PathBuf)> = vec![];
let manifest_dir = env!("CARGO_MANIFEST_DIR").to_string();
let base_dir = format!("{}/tests/{}", manifest_dir, subdir);
stderrs.append(&mut Self::collect_stderrs(&base_dir));
Self { stderrs, base_dir }
let base_dir = PathBuf::from(format!("{}/tests/{}", manifest_dir, subdir));
let outputs_dir = Self::outputs_dir(&base_dir).unwrap();
// stderrs.append(&mut Self::collect_stderrs(&outputs_dir));
let mut me = Self {
stderrs: Vec::new(),
base_dir,
outputs_dir,
};
me.collect_stderrs().unwrap();
me
}

fn stringify_fname(entry: Result<DirEntry, io::Error>, from_dir: &PathBuf) -> String {
Expand Down Expand Up @@ -54,19 +61,18 @@ impl UncompEnv {
)
}

fn collect_stderrs(from: &String) -> Vec<(PathBuf, PathBuf)> {
let dest_dir = PathBuf::from(&from);
let from_dir = PathBuf::from(format!("{}/{}", from, Self::version_group()));
fn collect_stderrs(&mut self) -> Result<(), io::Error> {
let dest_dir = &self.base_dir;
let from_dir = &self.outputs_dir;

if !from_dir.exists() {
return vec![];
panic!("Outputs directory '{}' doesn't exists.", from_dir.display());
}

if !from_dir.is_dir() {
panic!("'{}' is not a directory", from_dir.display());
}

let mut stderrs: Vec<(PathBuf, PathBuf)> = vec![];
for entry in std::fs::read_dir(&from_dir).expect(&format!("Failed to read '{}' directory", from_dir.display()))
{
let fname = Self::stringify_fname(entry, &from_dir);
Expand All @@ -76,11 +82,11 @@ impl UncompEnv {
let src_stderr = from_dir.join(&fname);
eprintln!("+ {} -> {}", src_stderr.display(), dest_stderr.display());
let _ = Self::copy_ok(&src_stderr, &dest_stderr);
stderrs.push((src_stderr, dest_stderr));
self.stderrs.push((src_stderr, dest_stderr));
}
}

stderrs
Ok(())
}

fn version_group() -> String {
Expand Down Expand Up @@ -122,21 +128,56 @@ impl UncompEnv {
.to_string()
}

fn check_for_new(&self) {
fn outputs_subdir() -> String {
#[allow(unused_mut)]
let mut groups = Vec::<String>::new();

#[cfg(feature = "serde")]
groups.push("serde".into());

#[cfg(feature = "sync")]
groups.push("sync".into());

#[cfg(feature = "async")]
groups.push("async".into());

#[cfg(feature = "diagnostics")]
groups.push("diagnostics".into());

if groups.len() > 0 {
format!("{}+{}", Self::version_group(), groups.join(","))
}
else {
Self::version_group()
}
}

fn outputs_dir(base_dir: &PathBuf) -> Result<PathBuf, io::Error> {
let outputs_dir = base_dir.join(Self::outputs_subdir());

if !outputs_dir.exists() {
std::fs::create_dir_all(&outputs_dir)?;
}

Ok(outputs_dir)
}

fn check_for_new(&self) -> Result<(), io::Error> {
let test_dir = PathBuf::from(&self.base_dir);
for entry in std::fs::read_dir(&test_dir).expect(&format!("Failed to read '{}' directory", test_dir.display()))
{
let fname = Self::stringify_fname(entry, &test_dir);

if fname.ends_with(".stderr") {
let version_dir = PathBuf::from(&self.base_dir).join(Self::version_group());
let src_stderr = test_dir.join(&fname);
let dest_stderr = version_dir.join(&fname);
let dest_stderr = self.outputs_dir.join(&fname);
eprintln!("> {} -> {}", fname, dest_stderr.display());
let _ = Self::copy_ok(&src_stderr, &dest_stderr);
let _ = Self::remove_ok(&src_stderr);
}
}

Ok(())
}
}

Expand All @@ -158,7 +199,7 @@ impl Drop for UncompEnv {

if try_build {
// Pick up any new .stderrs
self.check_for_new();
self.check_for_new().unwrap();
}

if with_failures {
Expand All @@ -172,17 +213,7 @@ fn failures() {
if std::env::var("__FIELDX_DEFAULT_TOOLCHAIN__").map_or(true, |v| v != "nightly") {
let test_env = UncompEnv::new("uncompilable");
let t = trybuild::TestCases::new();
t.compile_fail(format!("{}/*.rs", test_env.base_dir));
}
}

#[test]
#[cfg(feature = "serde")]
fn failures_serde() {
if std::env::var("__FIELDX_DEFAULT_TOOLCHAIN__").map_or(true, |v| v != "nightly") {
let test_env = UncompEnv::new("uncompilable_serde");
let t = trybuild::TestCases::new();
t.compile_fail(format!("{}/*.rs", test_env.base_dir));
t.compile_fail(format!("{}/*.rs", test_env.base_dir.display()));
}
}

Expand Down
1 change: 1 addition & 0 deletions fieldx/tests/inner_mut_copy_clone.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![cfg(feature = "sync")]
// Make sure inner_mut doesn't break copy and clone getters

use fieldx::fxstruct;
Expand Down
3 changes: 3 additions & 0 deletions fieldx/tests/no_default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ impl Plain {
}
}

#[cfg(feature = "sync")]
#[fxstruct(sync, get(public), builder, no_new)]
struct IsSync {
#[fieldx(set)]
Expand All @@ -53,6 +54,7 @@ struct IsSync {
b2: Bar,
}

#[cfg(feature = "sync")]
impl IsSync {
fn new(bar: Bar) -> Self {
Self {
Expand Down Expand Up @@ -90,6 +92,7 @@ fn plain() {
assert_eq!(plain.bar().note, "manual".to_string());
}

#[cfg(feature = "sync")]
#[test]
fn sync() {
let sync = IsSync::new(Bar { note: "manual".into() });
Expand Down
Loading

0 comments on commit a71b871

Please sign in to comment.