Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to zerocopy 0.8.0 #343

Merged
merged 2 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ resolver = "2"
members = ["anise", "anise-cli", "anise-gui", "anise-py"]

[workspace.package]
version = "0.4.5"
version = "0.5.0"
edition = "2021"
authors = ["Christopher Rabotin <[email protected]>"]
description = "ANISE provides a toolkit and files for Attitude, Navigation, Instrument, Spacecraft, and Ephemeris data. It's a modern replacement of NAIF SPICE file."
Expand Down Expand Up @@ -38,7 +38,7 @@ nalgebra = { version = "0.33", default-features = true, features = [
"serde-serialize",
] }
approx = "0.5.1"
zerocopy = { version = "0.7.26", features = ["derive"] }
zerocopy = { version = "0.8.0", features = ["derive"] }
bytes = "1.6.0"
snafu = { version = "0.8.0", features = ["backtrace"] }
lexical-core = "1.0.1"
Expand All @@ -50,7 +50,7 @@ serde = "1"
serde_derive = "1"
serde_dhall = "0.12"

anise = { version = "0.4.5", path = "anise", default-features = false }
anise = { version = "0.5.0", path = "anise", default-features = false }

[profile.bench]
debug = true
Expand Down
4 changes: 2 additions & 2 deletions anise-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ fn main() -> Result<(), CliErrors> {
}
} else {
// Load the header only
let file_record = FileRecord::read_from(&bytes[..FileRecord::SIZE]).unwrap();
let file_record = FileRecord::read_from_bytes(&bytes[..FileRecord::SIZE]).unwrap();
match file_record.identification().context(CliFileRecordSnafu)? {
"PCK" => {
info!("Loading {path_str:?} as DAF/PCK");
Expand Down Expand Up @@ -194,7 +194,7 @@ fn main() -> Result<(), CliErrors> {
fn read_and_record(path_str: PathBuf) -> Result<(bytes::Bytes, FileRecord), CliErrors> {
let bytes = file2heap!(path_str).context(AniseSnafu)?;
// Load the header only
let file_record = FileRecord::read_from(&bytes[..FileRecord::SIZE]).unwrap();
let file_record = FileRecord::read_from_bytes(&bytes[..FileRecord::SIZE]).unwrap();
Ok((bytes, file_record))
}

Expand Down
2 changes: 1 addition & 1 deletion anise/src/almanac/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ impl Almanac {

// Load the header only
if let Some(file_record_bytes) = bytes.get(..FileRecord::SIZE) {
let file_record = FileRecord::read_from(file_record_bytes).unwrap();
let file_record = FileRecord::read_from_bytes(file_record_bytes).unwrap();
if let Ok(fileid) = file_record.identification() {
return match fileid {
"PCK" => {
Expand Down
67 changes: 35 additions & 32 deletions anise/src/naif/daf/daf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use hifitime::Epoch;
use log::{debug, error, trace};
use snafu::ResultExt;

use zerocopy::AsBytes;
use zerocopy::IntoBytes;
use zerocopy::{FromBytes, Ref};

macro_rules! io_imports {
Expand Down Expand Up @@ -76,7 +76,7 @@ impl<R: NAIFSummaryRecord, W: MutKind> GenericDAF<R, W> {
}

pub fn file_record(&self) -> Result<FileRecord, DAFError> {
let file_record = FileRecord::read_from(
let file_record = FileRecord::read_from_bytes(
self.bytes
.get(..FileRecord::SIZE)
.ok_or_else(|| DecodingError::InaccessibleBytes {
Expand Down Expand Up @@ -108,7 +108,7 @@ impl<R: NAIFSummaryRecord, W: MutKind> GenericDAF<R, W> {
size: self.bytes.len(),
})
.context(DecodingNameSnafu { kind: R::NAME })?;
Ok(NameRecord::read_from(rcrd_bytes).unwrap())
Ok(NameRecord::read_from_bytes(rcrd_bytes).unwrap())
}

pub fn daf_summary(&self) -> Result<SummaryRecord, DAFError> {
Expand All @@ -123,8 +123,8 @@ impl<R: NAIFSummaryRecord, W: MutKind> GenericDAF<R, W> {
})
.context(DecodingSummarySnafu { kind: R::NAME })?;

SummaryRecord::read_from(&rcrd_bytes[..SummaryRecord::SIZE])
.ok_or(DecodingError::Casting)
SummaryRecord::read_from_bytes(&rcrd_bytes[..SummaryRecord::SIZE])
.or(Err(DecodingError::Casting))
.context(DecodingSummarySnafu { kind: R::NAME })
}

Expand Down Expand Up @@ -157,13 +157,15 @@ impl<R: NAIFSummaryRecord, W: MutKind> GenericDAF<R, W> {
};

// The summaries are defined in the same record as the DAF summary
Ok(match Ref::new_slice(&rcrd_bytes[SummaryRecord::SIZE..]) {
Some(data) => data.into_slice(),
None => &{
R::default();
[] as [R; 0]
Ok(
match Ref::<_, [R]>::from_bytes(&rcrd_bytes[SummaryRecord::SIZE..]) {
Ok(r) => Ref::into_ref(r),
Err(_) => &{
R::default();
[] as [R; 0]
},
},
})
)
}

/// Returns the summary given the name of the summary record
Expand Down Expand Up @@ -271,27 +273,28 @@ impl<R: NAIFSummaryRecord, W: MutKind> GenericDAF<R, W> {

let start = (this_summary.start_index() - 1) * DBL_SIZE;
let end = this_summary.end_index() * DBL_SIZE;
let data: &[f64] = Ref::new_slice(
match self
.bytes
.get(start..end)
.ok_or_else(|| DecodingError::InaccessibleBytes {
start,
end,
size: self.bytes.len(),
}) {
Ok(it) => it,
Err(source) => {
return Err(DAFError::DecodingData {
kind: R::NAME,
idx,
source,
})
}
},
)
.unwrap()
.into_slice();
let data: &[f64] = Ref::into_ref(
Ref::<&[u8], [f64]>::from_bytes(
match self
.bytes
.get(start..end)
.ok_or_else(|| DecodingError::InaccessibleBytes {
start,
end,
size: self.bytes.len(),
}) {
Ok(it) => it,
Err(source) => {
return Err(DAFError::DecodingData {
kind: R::NAME,
idx,
source,
})
}
},
)
.unwrap(),
);

// Convert it
S::from_f64_slice(data).context(DecodingDataSnafu { kind: R::NAME, idx })
Expand Down
4 changes: 2 additions & 2 deletions anise/src/naif/daf/datatypes/posvel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
*/

use core::fmt;
use zerocopy::{AsBytes, FromBytes, FromZeroes};
use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout};

use crate::{
math::Vector3,
naif::daf::{NAIFDataRecord, NAIFRecord},
};

#[derive(Copy, Clone, Default, AsBytes, FromBytes, FromZeroes, Debug)]
#[derive(Copy, Clone, Default, IntoBytes, FromBytes, KnownLayout, Immutable, Debug)]
#[repr(C)]
pub struct PositionVelocityRecord {
pub x_km: f64,
Expand Down
4 changes: 2 additions & 2 deletions anise/src/naif/daf/file_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use std::str::Utf8Error;

use snafu::prelude::*;
use zerocopy::{AsBytes, FromBytes, FromZeroes};
use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout};

use crate::naif::Endian;
use log::error;
Expand Down Expand Up @@ -42,7 +42,7 @@ pub enum FileRecordError {
EmptyRecord,
}

#[derive(Debug, Clone, FromBytes, FromZeroes, AsBytes, PartialEq)]
#[derive(Debug, Clone, FromBytes, KnownLayout, Immutable, IntoBytes, PartialEq)]
#[repr(C)]
pub struct FileRecord {
pub id_str: [u8; 8],
Expand Down
8 changes: 5 additions & 3 deletions anise/src/naif/daf/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{
use core::fmt::Display;
use hifitime::Epoch;
use snafu::prelude::*;
use zerocopy::{AsBytes, FromBytes};
use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout};

pub(crate) const RCRD_LEN: usize = 1024;
#[allow(clippy::module_inception)]
Expand All @@ -39,11 +39,13 @@ pub use summary_record::SummaryRecord;

use self::file_record::FileRecordError;

pub trait NAIFRecord: AsBytes + FromBytes + Sized + Default + Debug {
pub trait NAIFRecord:
IntoBytes + FromBytes + Sized + Default + Debug + Immutable + KnownLayout
{
const SIZE: usize = core::mem::size_of::<Self>();
}

pub trait NAIFSummaryRecord: NAIFRecord + Copy {
pub trait NAIFSummaryRecord: NAIFRecord + Copy + Immutable + KnownLayout {
type Error: 'static + std::error::Error;

fn start_index(&self) -> usize;
Expand Down
2 changes: 1 addition & 1 deletion anise/src/naif/daf/mut_daf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use crate::{
use bytes::BytesMut;
use hifitime::Epoch;
use snafu::ResultExt;
use zerocopy::AsBytes;
use zerocopy::IntoBytes;

impl<R: NAIFSummaryRecord> MutDAF<R> {
/// Parse the provided bytes as a SPICE Double Array File
Expand Down
4 changes: 2 additions & 2 deletions anise/src/naif/daf/name_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
* Documentation: https://nyxspace.com/
*/

use zerocopy::{AsBytes, FromBytes, FromZeroes};
use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout};

use crate::DBL_SIZE;
use log::warn;

use super::{DAFError, NAIFRecord, NAIFSummaryRecord, RCRD_LEN};

#[derive(AsBytes, Clone, Debug, FromZeroes, FromBytes)]
#[derive(IntoBytes, FromBytes, KnownLayout, Immutable, Clone, Debug)]
#[repr(C)]
pub struct NameRecord {
raw_names: [u8; RCRD_LEN],
Expand Down
4 changes: 2 additions & 2 deletions anise/src/naif/daf/summary_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
* Documentation: https://nyxspace.com/
*/

use zerocopy::{AsBytes, FromBytes, FromZeroes};
use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout};

use super::NAIFRecord;

#[derive(AsBytes, Clone, Copy, Debug, Default, FromZeroes, FromBytes)]
#[derive(IntoBytes, Clone, Copy, Debug, Default, FromBytes, KnownLayout, Immutable)]
#[repr(C)]
pub struct SummaryRecord {
next_record: f64,
Expand Down
4 changes: 2 additions & 2 deletions anise/src/naif/pck/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{
orientations::OrientationError,
};
use hifitime::Epoch;
use zerocopy::{AsBytes, FromBytes, FromZeroes};
use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout};

#[cfg(feature = "python")]
use pyo3::prelude::*;
Expand All @@ -22,7 +22,7 @@ use super::daf::DafDataType;

#[cfg_attr(feature = "python", pyclass)]
#[cfg_attr(feature = "python", pyo3(module = "anise.internals"))]
#[derive(Clone, Copy, Debug, Default, AsBytes, FromZeroes, FromBytes, PartialEq)]
#[derive(Clone, Copy, Debug, Default, IntoBytes, FromBytes, KnownLayout, Immutable, PartialEq)]
#[repr(C)]
pub struct BPCSummaryRecord {
pub start_epoch_et_s: f64,
Expand Down
4 changes: 2 additions & 2 deletions anise/src/naif/spk/summary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

use core::fmt;
use hifitime::{Epoch, TimeUnits};
use zerocopy::{AsBytes, FromBytes, FromZeroes};
use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout};

#[cfg(feature = "python")]
use pyo3::prelude::*;
Expand All @@ -23,7 +23,7 @@ use crate::{

#[cfg_attr(feature = "python", pyclass)]
#[cfg_attr(feature = "python", pyo3(module = "anise.internals"))]
#[derive(Clone, Copy, Debug, Default, AsBytes, FromZeroes, FromBytes, PartialEq)]
#[derive(Clone, Copy, Debug, Default, IntoBytes, Immutable, KnownLayout, FromBytes, PartialEq)]
#[repr(C)]
pub struct SPKSummaryRecord {
pub start_epoch_et_s: f64,
Expand Down
Loading