use std::collections::{HashMap, HashSet}; use std::sync::Arc;
use std::marker::PhantomData;
use std::io::Result as IoResult;
pub struct TestService {
pub name: String,
config: Config,
}
impl TestService {
pub fn new(name: String) -> Self {
let config = Config::default(); Self { name, config }
}
pub fn process(&self) -> String {
let result = self.get_config_name(); format!("Processing: {}", result)
}
fn get_config_name(&self) -> String {
self.config.get_display_name() }
}
mod inner {
pub struct InnerStruct;
}
pub use inner::InnerStruct;
const MAX_SIZE: usize = 1024;
pub const DEFAULT_NAME: &str = "defaults";
static mut COUNTER: u32 = 0;
static INSTANCE: std::sync::OnceLock<Config> = std::sync::OnceLock::new();
type Result<T> = std::result::Result<T, Error>;
type NodeId = u32;
pub type SharedData = Arc<Vec<u8>>;
type Handler<T> = Box<dyn Fn(T) -> Result<()> + Send + Sync>;
#[derive(Debug, Clone)]
pub struct Config {
pub name: String,
port: u16,
#[deprecated]
enabled: bool,
phantom: PhantomData<()>,
}
impl Config {
pub fn default() -> Self {
Self {
name: "default".to_string(),
port: 8080,
enabled: true,
phantom: PhantomData,
}
}
pub fn get_display_name(&self) -> String {
format!("Config: {}", self.name)
}
}
pub struct Point(f64, f64, f64);
pub struct Marker;
pub struct BorrowedData<'a> {
data: &'a str,
mutable: &'a mut [u8],
}
#[derive(Debug)]
pub enum Status {
Active,
Inactive { reason: String },
Pending(std::time::Duration),
Complex { id: u32, data: Vec<u8> },
}
pub enum Option2<T> {
Some(T),
None,
}
pub trait Parser {
type Input;
type Output;
type Error: std::error::Error;
const MAX_DEPTH: usize = 100;
fn parse(&self, input: Self::Input) -> Result<Self::Output>;
fn validate(&self, input: &Self::Input) -> bool {
true
}
fn new() -> Self where Self: Sized;
}
pub trait Container<T> {
fn add(&mut self, item: T);
fn get(&self, index: usize) -> Option<&T>;
fn iter(&self) -> impl Iterator<Item = &T>;
}
pub trait Lifecycle<'a> {
type Item: 'a;
fn process(&'a self) -> Self::Item;
}
impl Config {
pub const DEFAULT_PORT: u16 = 8080;
pub fn new(name: String) -> Self {
Self {
name,
port: Self::DEFAULT_PORT,
enabled: true,
phantom: PhantomData,
}
}
pub fn port(&self) -> u16 {
self.port
}
pub fn set_port(&mut self, port: u16) {
self.port = port;
}
pub fn into_name(self) -> String {
self.name
}
pub fn with_data<T>(&self, data: T) -> (Self, T)
where
T: Clone,
{
(self.clone(), data)
}
pub async fn connect(&self) -> Result<()> {
Ok(())
}
pub unsafe fn get_raw_ptr(&self) -> *const u8 {
&self.port as *const u16 as *const u8
}
}
impl Parser for Config {
type Input = String;
type Output = Config;
type Error = std::io::Error;
fn parse(&self, input: Self::Input) -> Result<Self::Output> {
Ok(Config::new(input))
}
fn new() -> Self {
Config::new(String::new())
}
}
pub struct GenericContainer<T, U = String>
where
T: Clone,
{
items: Vec<T>,
metadata: U,
}
impl<T, U> GenericContainer<T, U>
where
T: Clone + std::fmt::Debug,
U: Default,
{
pub fn new() -> Self {
Self {
items: Vec::new(),
metadata: U::default(),
}
}
pub fn add(&mut self, item: T) {
self.items.push(item);
}
}
impl<T> Container<T> for GenericContainer<T>
where
T: Clone,
{
fn add(&mut self, item: T) {
self.items.push(item);
}
fn get(&self, index: usize) -> Option<&T> {
self.items.get(index)
}
fn iter(&self) -> impl Iterator<Item = &T> {
self.items.iter()
}
}
pub fn complex_function<'a, T, U>(
reference: &'a str,
mutable: &mut Vec<T>,
owned: String,
generic: U,
closure: impl Fn() -> T,
) -> Result<&'a str>
where
T: Clone + 'a,
U: std::fmt::Debug,
{
mutable.push(closure());
Ok(reference)
}
pub async fn async_operation(url: &str) -> Result<String> {
let result = async {
url.to_uppercase()
};
Ok(url.to_string())
}
pub const fn const_function(x: u32) -> u32 {
x * 2
}
pub unsafe fn unsafe_operation(ptr: *mut u8) {
*ptr = 0;
}
pub fn returns_impl_trait() -> impl std::fmt::Display {
"hello"
}
pub fn takes_dyn_trait(parser: &dyn Parser<Input = String, Output = Config, Error = std::io::Error>) {
}
pub fn higher_ranked<F>(f: F)
where
F: for<'a> Fn(&'a str) -> &'a str,
{
f("test");
}
macro_rules! create_function {
($name:ident) => {
fn $name() {
println!("Function: {}", stringify!($name));
}
};
}
create_function!(generated_func);
#[repr(C)]
union MyUnion {
f1: u32,
f2: f32,
}
extern "C" {
fn external_function(x: i32) -> i32;
}
#[derive(Debug)]
pub struct Error {
message: String,
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.message)
}
}
impl std::error::Error for Error {}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_config() {
let config = Config::new("test".to_string());
assert_eq!(config.port(), Config::DEFAULT_PORT);
}
#[test]
#[ignore]
fn ignored_test() {
}
}
#[cfg(all(test, feature = "unstable"))]
mod benches {
use test::Bencher;
#[bench]
fn bench_create(b: &mut Bencher) {
b.iter(|| Config::new("bench".to_string()));
}
}
pub fn simple_embedding_test() {
println!("Simple embedding test function");
}
fn main() {
let service = TestService::new("test-app".to_string());
let result = service.process();
println!("Result: {}", result);
}