use crate::rudi::difactroy::find_bean_di_factroy;
use crate::rudi::{DiExample, find_bean_di_example};
#[cfg(test)]
mod tests {
use super::*;
use crate::rubase::get_self::GetSelf;
use crate::rulog;
use std::fmt::{Debug, Display};
use std::sync::Arc;
use crate::rucfg::find_bean_ru_config;
use crate::rulog::ru_log;
#[test]
fn test_01_example() {
let example = find_bean_di_example().unwrap();
rulog::info2("{}", example.id);
}
#[test]
fn test_02_make_di() {
find_bean_di_factroy().unwrap().lock().unwrap().make_di();
}
pub fn make_di_generic<T, F>(finder: F)
where
F: FnOnce() -> Option<Arc<T>>,
T: std::fmt::Display + Default + Clone, {
let example = finder().unwrap();
println!("{}", example);
let mut data = example;
println!("{}", data);
if let Some(mut_ref) = Arc::get_mut(&mut data) {
println!("modified: {}", mut_ref);
}
}
pub fn make_di_with_modifier<T, F, M>(finder: F, modifier: M)
where
F: FnOnce() -> Option<Arc<T>>,
M: FnOnce(&mut T),
T: std::fmt::Debug,
{
let example = finder().unwrap();
println!("{:#?}", example);
let mut data = example;
println!("{:#?}", data);
if let Some(mut_ref) = Arc::get_mut(&mut data) {
modifier(mut_ref);
println!("modified: {:#?}", mut_ref);
}
}
#[test]
fn test_03_make_di() {
make_di_with_modifier(
find_bean_di_example,
|data| {
data.id = 111;
}, );
}
fn get_di_value<T, F>(finder: F) -> Result<Arc<T>, Box<dyn std::error::Error>>
where
F: FnOnce() -> Option<Arc<T>>,
T: Display,
{
let example = finder().ok_or("Failed to find DI example")?;
println!("original: {}", example);
Ok(example)
}
fn modify_di_value<T>(
value: Arc<T>,
modifier: impl FnOnce(&mut T),
) -> Result<Arc<T>, Box<dyn std::error::Error>>
where
T: Display,
{
let mut data = value;
if let Some(mut_ref) = Arc::get_mut(&mut data) {
modifier(mut_ref);
println!("modified: {}", mut_ref);
}
Ok(data)
}
fn make_di_generic_result<T, F, M>(
finder: F,
modifier: M,
) -> Result<Arc<T>, Box<dyn std::error::Error>>
where
F: FnOnce() -> Option<Arc<T>>,
M: FnOnce(&mut T),
T: Debug,
{
let example = finder().ok_or("Failed to find DI example")?;
let mut data = example;
if let Some(mut_ref) = Arc::get_mut(&mut data) {
modifier(mut_ref);
}
Ok(data)
}
#[test]
fn test_04_make_di() {
let mut ff = find_bean_di_example().unwrap().get_self();
ff.id = 222;
println!("modified: {:#?}", ff);
}
#[test]
fn test_05_make_di() {
let de = find_bean_di_factroy_mutex().unwrap();
let mut data = de.lock().unwrap();
data.id = 222;
println!("modified: {:#?}", data);
}
#[test]
fn test_07_make_di() {
ru_log::info("/conf handler called");
let c = find_bean_ru_config().unwrap().lock().unwrap().read_clone();
let json_str = serde_json::to_string(&c.web).unwrap();
ru_log::info2("conf response length:", json_str.len());
}
}
use crate::rudi::dicontainer::direg;
use ctor::ctor;
use std::sync::{Arc, LazyLock, Mutex};
use uuid::Uuid;
const SINGLE_BEAN_NAME_mutex: &str =
"mutex1::*rudi::DiExample::difactroy:DiFactroy:75b5f715-2ce7-4322-9429-0e9b0cebbf23:single}";
pub fn find_bean_di_factroy_mutex() -> Option<Arc<Mutex<DiExample>>> {
direg::get_bean::<Mutex<DiExample>>(SINGLE_BEAN_NAME_mutex)
}
pub fn register_bean_difactroy_mutex() {
direg::register_singleton(SINGLE_BEAN_NAME_mutex, DiExample::new_mutex);
}
pub trait BeanSingleton: Sized + Send + Sync + 'static {
const BEAN_NAME: &'static str;
fn bean_name() -> &'static str {
std::any::type_name::<Self>() }
fn new_bean() -> Self;
fn find_bean() -> Option<Arc<Mutex<Self>>> {
direg::get_bean::<Mutex<Self>>(Self::BEAN_NAME)
}
fn register_bean() {
direg::register_singleton(Self::BEAN_NAME, || Mutex::new(Self::new_bean()));
}
}