-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Smash a couple of thinkos and wrongdoings
- Loading branch information
Showing
10 changed files
with
243 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,79 @@ | ||
use fieldx::fxstruct; | ||
use std::sync::Arc; | ||
|
||
#[fxstruct(mode(async), fallible(off, error(String)))] | ||
struct Foo { | ||
struct Foo<const FAIL: bool = false> { | ||
#[fieldx(lazy, fallible)] | ||
never_ok: i32, | ||
ok: i32, | ||
|
||
#[fieldx(lazy, fallible, get_mut)] | ||
#[fieldx(lazy, fallible, get(copy), get_mut, set)] | ||
writable: u32, | ||
|
||
#[fieldx(lazy, fallible, get(clone))] | ||
shared: Arc<String>, | ||
} | ||
|
||
impl Foo { | ||
async fn build_never_ok(&self) -> Result<i32, String> { | ||
Err("will never be there".to_string()) | ||
impl<const FAIL: bool> Foo<FAIL> { | ||
async fn build_ok(&self) -> Result<i32, String> { | ||
if FAIL { | ||
Err("will never be there".to_string()) | ||
} | ||
else { | ||
Ok(-42) | ||
} | ||
} | ||
|
||
async fn build_writable(&self) -> Result<u32, String> { | ||
Ok(12) | ||
if FAIL { | ||
Err("no value".to_string()) | ||
} | ||
else { | ||
Ok(12) | ||
} | ||
} | ||
|
||
async fn build_shared(&self) -> Result<Arc<String>, String> { | ||
if FAIL { | ||
Err("this is a failed outcome".to_string()) | ||
} | ||
else { | ||
Ok(Arc::new("shared".to_string())) | ||
} | ||
} | ||
} | ||
|
||
#[tokio::test] | ||
async fn fallible() -> Result<(), Box<dyn std::error::Error>> { | ||
let foo = Foo::new(); | ||
assert!(foo.never_ok().await.is_err()); | ||
assert_eq!(*foo.never_ok().await.unwrap_err(), String::from("will never be there")); | ||
async fn fallible_ok() -> Result<(), Box<dyn std::error::Error>> { | ||
let foo = Foo::<false>::new(); | ||
|
||
assert!(*foo.ok().await? == -42); | ||
|
||
assert_eq!(*foo.writable().await?, 12); | ||
assert_eq!(foo.writable().await?, 12); | ||
*foo.writable_mut().await? = 42; | ||
assert_eq!(*foo.writable().await?, 42); | ||
assert_eq!(foo.writable().await?, 42); | ||
|
||
assert_eq!(*foo.shared().await?, "shared".to_string()); | ||
|
||
Ok(()) | ||
} | ||
|
||
#[tokio::test] | ||
async fn fallible_error() -> Result<(), Box<dyn std::error::Error>> { | ||
let foo = Foo::<true>::new(); | ||
|
||
assert!(foo.ok().await.is_err()); | ||
assert_eq!(*foo.ok().await.unwrap_err(), String::from("will never be there")); | ||
|
||
assert!(foo.writable().await.is_err()); | ||
assert_eq!(foo.writable().await.unwrap_err(), "no value".to_string()); | ||
foo.set_writable(42).await; | ||
assert_eq!(foo.writable().await?, 42); | ||
|
||
assert!(foo.shared().await.is_err()); | ||
assert_eq!( | ||
*foo.shared().await.unwrap_err(), | ||
String::from("this is a failed outcome") | ||
); | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,76 @@ | ||
use fieldx::fxstruct; | ||
use std::rc::Rc; | ||
|
||
#[fxstruct(mode(plain), fallible(off, error(String)))] | ||
struct Foo { | ||
struct Foo<const FAIL: bool = false> { | ||
#[fieldx(lazy, fallible)] | ||
never_ok: i32, | ||
ok: i32, | ||
|
||
#[fieldx(lazy, fallible, get_mut)] | ||
#[fieldx(lazy, fallible, get(copy), get_mut, set)] | ||
writable: u32, | ||
|
||
#[fieldx(lazy, fallible, get(clone))] | ||
shared: Rc<String>, | ||
} | ||
|
||
impl Foo { | ||
fn build_never_ok(&self) -> Result<i32, String> { | ||
Err("will never be there".to_string()) | ||
impl<const FAIL: bool> Foo<FAIL> { | ||
fn build_ok(&self) -> Result<i32, String> { | ||
if FAIL { | ||
Err("will never be there".to_string()) | ||
} | ||
else { | ||
Ok(-42) | ||
} | ||
} | ||
|
||
fn build_writable(&self) -> Result<u32, String> { | ||
Ok(12) | ||
if FAIL { | ||
Err("no value".to_string()) | ||
} | ||
else { | ||
Ok(12) | ||
} | ||
} | ||
|
||
fn build_shared(&self) -> Result<Rc<String>, String> { | ||
if FAIL { | ||
Err("this is a failed outcome".to_string()) | ||
} | ||
else { | ||
Ok(Rc::new("shared".to_string())) | ||
} | ||
} | ||
} | ||
|
||
#[test] | ||
fn fallible() -> Result<(), Box<dyn std::error::Error>> { | ||
let mut foo = Foo::new(); | ||
assert!(foo.never_ok().is_err()); | ||
assert_eq!(*foo.never_ok().unwrap_err(), String::from("will never be there")); | ||
fn fallible_ok() -> Result<(), Box<dyn std::error::Error>> { | ||
let mut foo = Foo::<false>::new(); | ||
|
||
assert!(*foo.ok()? == -42); | ||
|
||
assert_eq!(*foo.writable()?, 12); | ||
assert_eq!(foo.writable()?, 12); | ||
*foo.writable_mut()? = 42; | ||
assert_eq!(*foo.writable()?, 42); | ||
assert_eq!(foo.writable()?, 42); | ||
|
||
assert_eq!(*foo.shared()?, "shared".to_string()); | ||
|
||
Ok(()) | ||
} | ||
|
||
#[test] | ||
fn fallible_error() -> Result<(), Box<dyn std::error::Error>> { | ||
let mut foo = Foo::<true>::new(); | ||
|
||
assert!(foo.ok().is_err()); | ||
assert_eq!(*foo.ok().unwrap_err(), String::from("will never be there")); | ||
|
||
assert!(foo.writable().is_err()); | ||
assert_eq!(foo.writable().unwrap_err(), "no value".to_string()); | ||
foo.set_writable(42); | ||
assert_eq!(foo.writable()?, 42); | ||
|
||
assert!(foo.shared().is_err()); | ||
assert_eq!(*foo.shared().unwrap_err(), String::from("this is a failed outcome")); | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,76 @@ | ||
use fieldx::fxstruct; | ||
use std::sync::Arc; | ||
|
||
#[fxstruct(mode(sync), fallible(off, error(String)))] | ||
struct Foo { | ||
struct Foo<const FAIL: bool = false> { | ||
#[fieldx(lazy, fallible)] | ||
never_ok: i32, | ||
ok: i32, | ||
|
||
#[fieldx(lazy, fallible, get_mut)] | ||
#[fieldx(lazy, fallible, get(copy), get_mut, set)] | ||
writable: u32, | ||
|
||
#[fieldx(lazy, fallible, get(clone))] | ||
shared: Arc<String>, | ||
} | ||
|
||
impl Foo { | ||
fn build_never_ok(&self) -> Result<i32, String> { | ||
Err("will never be there".to_string()) | ||
impl<const FAIL: bool> Foo<FAIL> { | ||
fn build_ok(&self) -> Result<i32, String> { | ||
if FAIL { | ||
Err("will never be there".to_string()) | ||
} | ||
else { | ||
Ok(-42) | ||
} | ||
} | ||
|
||
fn build_writable(&self) -> Result<u32, String> { | ||
Ok(12) | ||
if FAIL { | ||
Err("no value".to_string()) | ||
} | ||
else { | ||
Ok(12) | ||
} | ||
} | ||
|
||
fn build_shared(&self) -> Result<Arc<String>, String> { | ||
if FAIL { | ||
Err("this is a failed outcome".to_string()) | ||
} | ||
else { | ||
Ok(Arc::new("shared".to_string())) | ||
} | ||
} | ||
} | ||
|
||
#[test] | ||
fn fallible() -> Result<(), Box<dyn std::error::Error>> { | ||
let foo = Foo::new(); | ||
assert!(foo.never_ok().is_err()); | ||
assert_eq!(*foo.never_ok().unwrap_err(), String::from("will never be there")); | ||
fn fallible_ok() -> Result<(), Box<dyn std::error::Error>> { | ||
let foo = Foo::<false>::new(); | ||
|
||
assert!(*foo.ok()? == -42); | ||
|
||
assert_eq!(*foo.writable()?, 12); | ||
assert_eq!(foo.writable()?, 12); | ||
*foo.writable_mut()? = 42; | ||
assert_eq!(*foo.writable()?, 42); | ||
assert_eq!(foo.writable()?, 42); | ||
|
||
assert_eq!(*foo.shared()?, "shared".to_string()); | ||
|
||
Ok(()) | ||
} | ||
|
||
#[test] | ||
fn fallible_error() -> Result<(), Box<dyn std::error::Error>> { | ||
let foo = Foo::<true>::new(); | ||
|
||
assert!(foo.ok().is_err()); | ||
assert_eq!(*foo.ok().unwrap_err(), String::from("will never be there")); | ||
|
||
assert!(foo.writable().is_err()); | ||
assert_eq!(foo.writable().unwrap_err(), "no value".to_string()); | ||
foo.set_writable(42); | ||
assert_eq!(foo.writable()?, 42); | ||
|
||
assert!(foo.shared().is_err()); | ||
assert_eq!(*foo.shared().unwrap_err(), String::from("this is a failed outcome")); | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.