mirror of
https://github.com/cberner/raptorq.git
synced 2024-06-16 03:49:00 +00:00
5a720829fa
* feat: support no_std `metal` feature supports `no_std` in configuration `default-features = false, features = ["metal"]`. Float calculation is done via `micromath` crate. All previously available functionality remains under default `std` feature. Some tweaking of `python` and `wasm` features was done to compile tests. * feat: get rid of floats (#2) * feat: remove conversion to f64, fix features * chore: uncomment symbols_required checker, fmt * revert: add cdylib target for python support * fix: generalize crate type --------- Co-authored-by: varovainen <99664267+varovainen@users.noreply.github.com>
76 lines
1.9 KiB
Rust
76 lines
1.9 KiB
Rust
use std::vec::Vec;
|
|
|
|
use crate::base::{EncodingPacket, ObjectTransmissionInformation};
|
|
use crate::decoder::Decoder as DecoderNative;
|
|
use crate::encoder::Encoder as EncoderNative;
|
|
use pyo3::prelude::*;
|
|
use pyo3::types::*;
|
|
|
|
#[pyclass]
|
|
pub struct Encoder {
|
|
encoder: EncoderNative,
|
|
}
|
|
|
|
#[pymethods]
|
|
impl Encoder {
|
|
#[staticmethod]
|
|
pub fn with_defaults(data: &PyBytes, maximum_transmission_unit: u16) -> PyResult<Encoder> {
|
|
let encoder = EncoderNative::with_defaults(data.as_bytes(), maximum_transmission_unit);
|
|
Ok(Encoder { encoder })
|
|
}
|
|
|
|
pub fn get_encoded_packets<'p>(
|
|
&self,
|
|
py: Python<'p>,
|
|
repair_packets_per_block: u32,
|
|
) -> PyResult<Vec<&'p PyBytes>> {
|
|
let packets: Vec<&PyBytes> = self
|
|
.encoder
|
|
.get_encoded_packets(repair_packets_per_block)
|
|
.iter()
|
|
.map(|packet| PyBytes::new(py, &packet.serialize()))
|
|
.collect();
|
|
|
|
Ok(packets)
|
|
}
|
|
}
|
|
|
|
#[pyclass]
|
|
pub struct Decoder {
|
|
decoder: DecoderNative,
|
|
}
|
|
|
|
#[pymethods]
|
|
impl Decoder {
|
|
#[staticmethod]
|
|
pub fn with_defaults(
|
|
transfer_length: u64,
|
|
maximum_transmission_unit: u16,
|
|
) -> PyResult<Decoder> {
|
|
let config = ObjectTransmissionInformation::with_defaults(
|
|
transfer_length,
|
|
maximum_transmission_unit,
|
|
);
|
|
let decoder = DecoderNative::new(config);
|
|
Ok(Decoder { decoder })
|
|
}
|
|
|
|
pub fn decode<'p>(
|
|
&mut self,
|
|
py: Python<'p>,
|
|
packet: &PyBytes,
|
|
) -> PyResult<Option<&'p PyBytes>> {
|
|
let result = self
|
|
.decoder
|
|
.decode(EncodingPacket::deserialize(packet.as_bytes()));
|
|
Ok(result.map(|data| PyBytes::new(py, &data)))
|
|
}
|
|
}
|
|
|
|
#[pymodule]
|
|
pub fn raptorq(_py: Python, m: &PyModule) -> PyResult<()> {
|
|
m.add_class::<Encoder>()?;
|
|
m.add_class::<Decoder>()?;
|
|
Ok(())
|
|
}
|