iowo/crates/prowocessing/src/experimental/trait_based/data/raw.rs

59 lines
1.2 KiB
Rust
Raw Normal View History

2024-07-08 20:48:56 +02:00
//! Dynamic data storage and transfer types for use in [`io`]
/// Owned data type, for use mostly in outputs and storage
#[derive(Clone, Debug)]
2024-07-08 20:48:56 +02:00
pub enum Data {
String(String),
Int(i32),
}
2024-07-08 20:48:56 +02:00
impl From<String> for Data {
fn from(value: String) -> Self {
Self::String(value)
}
}
2024-07-08 20:48:56 +02:00
impl From<i32> for Data {
fn from(value: i32) -> Self {
Self::Int(value)
}
}
/// Unowned data type, for inputs into runner functions
2024-07-08 20:48:56 +02:00
#[derive(Clone, Copy, Debug)]
pub enum DataRef<'a> {
String(&'a str),
Int(i32),
}
2024-07-08 20:48:56 +02:00
impl DataRef<'_> {
/// converts itself to `OwnedData`
2024-07-08 20:48:56 +02:00
pub fn to_owned_data(&self) -> Data {
match self {
2024-07-08 20:48:56 +02:00
DataRef::String(s) => (*s).to_owned().into(),
DataRef::Int(i) => (*i).into(),
}
}
}
2024-07-08 20:48:56 +02:00
impl<'a> From<&'a str> for DataRef<'a> {
fn from(value: &'a str) -> Self {
Self::String(value)
}
}
2024-07-08 20:48:56 +02:00
impl From<i32> for DataRef<'_> {
fn from(value: i32) -> Self {
Self::Int(value)
}
}
2024-07-08 20:48:56 +02:00
impl<'a> From<&'a Data> for DataRef<'a> {
fn from(value: &'a Data) -> Self {
match value {
2024-07-08 20:48:56 +02:00
Data::String(s) => DataRef::String(s),
Data::Int(i) => DataRef::Int(*i),
}
}
}