Ut
Wraps asynchronous functions in order to make them synchronous based on if a "sync" feature is enabled. This is useful when writting http clients as you can write async methods and wrap them for use in synchronous code bases automatically.
Usage
[]
= "0.2.0"
Then in the crate that you want to have synchronous functions created for you you create a sync feature. When this feature is enabled ut will create synchronous functions on what you have wrapped.
you can either:
- Replace your asynchronous function with a synchronous one
- Clone your asynchronous function with a synchronous one ending in blocking
- Clone all methods in an impl with synchronous ones
Replacing async functions
You can replace your asynchronous function with a synchronous one by doing something like the following:
async
Cloning async functions
You can clone your asynchronous function with a synchronous one ending in blocking by doing something like the following:
let out = foo_blocking;
assert_eq!
async
Cloning async methods in implementations
You can clone all methods in an impl with synchronous ones by using ut::clone_impl. This is useful when you want to support both async and sync functions in a struct implementation.
// The original struct
// You also need to create the struct to place the cloned impls in
// This is done so you can choose what structs/impls to clone/wrap
// The cloned structs/impls should end in Blocking
// The original struct with async functions
;
// The blocking struct that we are cloning impls into
// You have to create this so you can add custom derives
;
// The async impls that you want to wrap
// All methods within this impl must be async
let example = default;
let out = example.fooers.foo;
assert_eq!;
let out = example.fooers.bar;
assert_eq!
Currently the wrapping is very naive and simply wraps the function in tokio::main. This is likely more expensive then it needs to be and I hope to make it more efficient later.