This repository has been archived on 2024-05-26. You can view files and clone it, but cannot push or open issues or pull requests.
trixy/src/lib.rs

98 lines
2.8 KiB
Rust
Raw Normal View History

/*
* Copyright (C) 2023 - 2024:
* The Trinitrix Project <soispha@vhack.eu, antifallobst@systemausfall.org>
*
* This file is part of the Trixy crate for Trinitrix.
*
* Trixy is free software: you can redistribute it and/or modify
* it under the terms of the Lesser GNU General Public License as
* published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* and the Lesser GNU General Public License along with this program.
* If not, see <https://www.gnu.org/licenses/>.
*/
2023-12-26 11:18:58 +00:00
pub mod types {
pub use trixy_types::*;
}
2023-12-25 21:23:52 +00:00
2023-12-26 11:18:58 +00:00
pub mod macros {
pub use trixy_macros::*;
2023-12-25 21:23:52 +00:00
}
feat(treewide): Add broken Vec<E>, Result<T,E> and Option<T> types to c api These are implemented right now by simply casting the generic arguments to void pointers and providing a `type_id` field in every struct denoting the original type. This implementation, whilst being extremely unwieldy to work with on the c side, also fails in a lot of fundamental ways: 1. The `type_id` enum *can* never really support user defined types because we would already need it to provide the c to rust value conversion. 2. Even without custom user types the type conversion is extremely hard to correctly implement in a somewhat performant way: A vector passed from c code to rust would need to completely reallocated *one element at a time*. And this only works if the c side has correctly cast the void pointer to the vectors data before accessing it, as any other way would have lead to possible unaligned data (which the rust side had to account for). 3. The c api is just simply bad in this state: You have to always look at the Trixy file to even be able to deal with the data the api returns (that is: There is no mention of a results generics in the c header). Additionally the question arises if these types should even be leaked into the c code because than c just becomes a worse version of rust, which undermines the whole reason of providing a c api in the first place. One way to fix all these issues would be to change the way generics are handled by using unions instead of the void pointer and trying to avoid leaking these rust types in c as far as possible. This approach would require a lot less binding code (both on the c and rust side), but also would make the rust based c-header-gen-code harder, as it would then be required to turn a `Vec<String>` to a `char **` (and obviously a whole wrapper struct with size and string length), whilst turning a `Vec<char>` to a `char*` differentiating it from a `string_t`.
2023-12-28 09:28:58 +00:00
pub mod oneshot {
//! This module provides a synced version of tokio's oneshot channel.
//! Notably this is implemented as wrapper around std's mpsc channel, which closes after one received value
feat(treewide): Add broken Vec<E>, Result<T,E> and Option<T> types to c api These are implemented right now by simply casting the generic arguments to void pointers and providing a `type_id` field in every struct denoting the original type. This implementation, whilst being extremely unwieldy to work with on the c side, also fails in a lot of fundamental ways: 1. The `type_id` enum *can* never really support user defined types because we would already need it to provide the c to rust value conversion. 2. Even without custom user types the type conversion is extremely hard to correctly implement in a somewhat performant way: A vector passed from c code to rust would need to completely reallocated *one element at a time*. And this only works if the c side has correctly cast the void pointer to the vectors data before accessing it, as any other way would have lead to possible unaligned data (which the rust side had to account for). 3. The c api is just simply bad in this state: You have to always look at the Trixy file to even be able to deal with the data the api returns (that is: There is no mention of a results generics in the c header). Additionally the question arises if these types should even be leaked into the c code because than c just becomes a worse version of rust, which undermines the whole reason of providing a c api in the first place. One way to fix all these issues would be to change the way generics are handled by using unions instead of the void pointer and trying to avoid leaking these rust types in c as far as possible. This approach would require a lot less binding code (both on the c and rust side), but also would make the rust based c-header-gen-code harder, as it would then be required to turn a `Vec<String>` to a `char **` (and obviously a whole wrapper struct with size and string length), whilst turning a `Vec<char>` to a `char*` differentiating it from a `string_t`.
2023-12-28 09:28:58 +00:00
use std::{
mem,
sync::mpsc::{self, RecvError, SendError, TryRecvError},
};
#[derive(Clone, Debug)]
pub struct Sender<T> {
channel: mpsc::Sender<T>,
}
#[derive(Debug)]
pub struct Receiver<T> {
channel: mpsc::Receiver<T>,
last_value: Option<T>,
}
pub fn channel<T>() -> (Sender<T>, Receiver<T>) {
let (tx, rx) = mpsc::channel();
(
Sender { channel: tx },
Receiver {
channel: rx,
last_value: None,
},
)
}
impl<T> Sender<T> {
pub fn send(&self, input: T) -> Result<(), SendError<T>> {
self.channel.send(input)
}
}
impl<T> Receiver<T> {
pub fn try_recv(&mut self) -> Result<bool, TryRecvError> {
match self.channel.try_recv() {
Ok(ok) => {
self.close();
self.last_value = Some(ok);
Ok(true)
}
Err(err) => Err(err),
}
}
pub fn close(&mut self) {
let (_, recv) = mpsc::channel();
let channel = mem::replace(&mut self.channel, recv);
drop(channel);
}
pub fn recv(mut self) -> Result<T, RecvError> {
match self.channel.recv() {
Ok(ok) => {
self.close();
Ok(ok)
}
Err(err) => {
if let Some(val) = self.last_value {
Ok(val)
} else {
Err(err)
}
}
}
}
}
}