Skip to content

Commit

Permalink
Use io:: prefix for IO structs and traits
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeyBF committed Dec 8, 2024
1 parent ec5f98b commit 3ab84b7
Show file tree
Hide file tree
Showing 13 changed files with 94 additions and 110 deletions.
19 changes: 7 additions & 12 deletions ext/crates/algebra/src/algebra/pair_algebra.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::{combinatorics, Algebra};

type HashMap<K, V> = hashbrown::HashMap<K, V, std::hash::BuildHasherDefault<FxHasher>>;

use std::io::{Read, Write};
use std::io;

/// A lift of an algebra to a split pair algebra. See module introduction for more.
pub trait PairAlgebra: Algebra {
Expand Down Expand Up @@ -82,13 +82,12 @@ pub trait PairAlgebra: Algebra {
/// indecomposable.
fn p_tilde(&self) -> usize;

fn element_to_bytes(&self, elt: &Self::Element, buffer: &mut impl Write)
-> std::io::Result<()>;
fn element_to_bytes(&self, elt: &Self::Element, buffer: &mut impl io::Write) -> io::Result<()>;
fn element_from_bytes(
&self,
degree: i32,
buffer: &mut impl Read,
) -> std::io::Result<Self::Element>;
buffer: &mut impl io::Read,
) -> io::Result<Self::Element>;
}

use std::cell::RefCell;
Expand Down Expand Up @@ -299,11 +298,7 @@ impl PairAlgebra for MilnorAlgebra {
}
}

fn element_to_bytes(
&self,
elt: &Self::Element,
buffer: &mut impl Write,
) -> std::io::Result<()> {
fn element_to_bytes(&self, elt: &Self::Element, buffer: &mut impl io::Write) -> io::Result<()> {
elt.twos.to_bytes(buffer)?;
for row in &elt.ys {
for v in row {
Expand All @@ -316,8 +311,8 @@ impl PairAlgebra for MilnorAlgebra {
fn element_from_bytes(
&self,
degree: i32,
buffer: &mut impl Read,
) -> std::io::Result<Self::Element> {
buffer: &mut impl io::Read,
) -> io::Result<Self::Element> {
let p = self.prime();
assert_eq!(p, TWO);

Expand Down
14 changes: 7 additions & 7 deletions ext/crates/algebra/src/algebra/steenrod_algebra.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::io::{Read, Write};
use std::io;

use anyhow::anyhow;
use fp::{
Expand Down Expand Up @@ -263,16 +263,16 @@ impl crate::pair_algebra::PairAlgebra for AdemAlgebra {
fn element_to_bytes(
&self,
_elt: &Self::Element,
_buffer: &mut impl Write,
) -> std::io::Result<()> {
_buffer: &mut impl io::Write,
) -> io::Result<()> {
unimplemented!()
}

fn element_from_bytes(
&self,
_degree: i32,
_buffer: &mut impl Read,
) -> std::io::Result<Self::Element> {
_buffer: &mut impl io::Read,
) -> io::Result<Self::Element> {
unimplemented!()
}
}
Expand All @@ -286,8 +286,8 @@ impl crate::pair_algebra::PairAlgebra for SteenrodAlgebra {
fn sigma_multiply_basis(&self, result: &mut Self::Element, coeff: u32, r_degree: i32, r_idx: usize, s_degree: i32, s_idx: usize);
fn sigma_multiply(&self, result: &mut Self::Element, coeff: u32, r_degree: i32, r: FpSlice, s_degree: i32, s: FpSlice);
fn a_multiply(&self, result: FpSliceMut, coeff: u32, r_degree: i32, r: FpSlice, s_degree: i32, s: &Self::Element);
fn element_to_bytes(&self, elt: &Self::Element, buffer: &mut impl Write) -> std::io::Result<()>;
fn element_from_bytes(&self, degree: i32, buffer: &mut impl Read) -> std::io::Result<Self::Element>;
fn element_to_bytes(&self, elt: &Self::Element, buffer: &mut impl io::Write) -> io::Result<()>;
fn element_from_bytes(&self, degree: i32, buffer: &mut impl io::Read) -> io::Result<Self::Element>;
}

fn element_is_zero(elt: &Self::Element) -> bool {
Expand Down
20 changes: 10 additions & 10 deletions ext/crates/chart/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![deny(clippy::use_self)]

use std::{collections::HashMap, fmt::Display, io::Write};
use std::{collections::HashMap, fmt::Display, io};

#[rustfmt::skip]
const PATTERNS: [(f32, &[(f32, f32)]); 12] = [
Expand Down Expand Up @@ -105,14 +105,14 @@ pub trait Backend {
}
}

pub struct SvgBackend<T: Write> {
pub struct SvgBackend<T: io::Write> {
out: T,
max_x: i32,
max_y: i32,
num_nodes: HashMap<(i32, i32), usize>,
}

impl<T: Write> SvgBackend<T> {
impl<T: io::Write> SvgBackend<T> {
const GRID_WIDTH: i32 = 20;
const MARGIN: i32 = 30;
const STYLES: &'static str = r#"
Expand Down Expand Up @@ -200,8 +200,8 @@ impl<T: Write> SvgBackend<T> {
}
}

impl<T: Write> Backend for SvgBackend<T> {
type Error = std::io::Error;
impl<T: io::Write> Backend for SvgBackend<T> {
type Error = io::Error;

const EXT: &'static str = "svg";

Expand Down Expand Up @@ -304,20 +304,20 @@ impl<T: Write> Backend for SvgBackend<T> {
}
}

impl<T: Write> Drop for SvgBackend<T> {
impl<T: io::Write> Drop for SvgBackend<T> {
fn drop(&mut self) {
writeln!(self.out, "</svg>").unwrap();
}
}

pub struct TikzBackend<T: Write> {
pub struct TikzBackend<T: io::Write> {
out: T,
max_x: i32,
max_y: i32,
num_nodes: HashMap<(i32, i32), usize>,
}

impl<T: Write> TikzBackend<T> {
impl<T: io::Write> TikzBackend<T> {
const HEADER: &'static str = r"\begin{tikzpicture}[
major-grid/.style={ opacity = 0.2 },
grid/.style={ opacity = 0.1 },
Expand Down Expand Up @@ -350,7 +350,7 @@ impl<T: Write> TikzBackend<T> {
}
}

impl<T: Write> Backend for TikzBackend<T> {
impl<T: io::Write> Backend for TikzBackend<T> {
type Error = std::io::Error;

const EXT: &'static str = "tex";
Expand Down Expand Up @@ -432,7 +432,7 @@ impl<T: Write> Backend for TikzBackend<T> {
}
}

impl<T: Write> Drop for TikzBackend<T> {
impl<T: io::Write> Drop for TikzBackend<T> {
fn drop(&mut self) {
writeln!(self.out, r#"\end{{tikzpicture}}"#).unwrap();
}
Expand Down
4 changes: 2 additions & 2 deletions ext/crates/fp/build.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::io::Error;
use std::io;

use build_const::ConstWriter;

type Limb = u64;

fn main() -> Result<(), Error> {
fn main() -> io::Result<()> {
// We want primes up to 2^8 - 1, because those will be the characteristics of the fields that
// have degree at least 2 and order at most 2^16 - 1. We will use PRIME_TO_INDEX_MAP when
// computing Zech logarithms.
Expand Down
13 changes: 6 additions & 7 deletions ext/crates/fp/src/matrix/matrix_inner.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::{
fmt,
io::{Read, Write},
fmt, io,
ops::{Index, IndexMut},
};

Expand Down Expand Up @@ -83,8 +82,8 @@ impl Matrix {
p: ValidPrime,
rows: usize,
columns: usize,
data: &mut impl Read,
) -> std::io::Result<Self> {
data: &mut impl io::Read,
) -> io::Result<Self> {
let mut vectors: Vec<FpVector> = Vec::with_capacity(rows);
for _ in 0..rows {
vectors.push(FpVector::from_bytes(p, columns, data)?);
Expand All @@ -97,15 +96,15 @@ impl Matrix {
})
}

pub fn to_bytes(&self, data: &mut impl Write) -> std::io::Result<()> {
pub fn to_bytes(&self, data: &mut impl io::Write) -> io::Result<()> {
for v in &self.vectors {
v.to_bytes(data)?;
}
Ok(())
}

/// Read a vector of `isize`
pub(crate) fn write_pivot(v: &[isize], buffer: &mut impl Write) -> std::io::Result<()> {
pub(crate) fn write_pivot(v: &[isize], buffer: &mut impl io::Write) -> io::Result<()> {
if cfg!(all(target_endian = "little", target_pointer_width = "64")) {
unsafe {
let buf: &[u8] = std::slice::from_raw_parts(v.as_ptr() as *const u8, v.len() * 8);
Expand All @@ -121,7 +120,7 @@ impl Matrix {
}

/// Read a vector of `isize` of length `dim`.
pub(crate) fn read_pivot(dim: usize, data: &mut impl Read) -> std::io::Result<Vec<isize>> {
pub(crate) fn read_pivot(dim: usize, data: &mut impl io::Read) -> io::Result<Vec<isize>> {
if cfg!(all(target_endian = "little", target_pointer_width = "64")) {
let mut image = vec![0; dim];
unsafe {
Expand Down
12 changes: 6 additions & 6 deletions ext/crates/fp/src/matrix/quasi_inverse.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::io::{Read, Write};
use std::io;

use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use itertools::Itertools;
Expand Down Expand Up @@ -43,7 +43,7 @@ impl QuasiInverse {
}
}

pub fn to_bytes(&self, buffer: &mut impl Write) -> std::io::Result<()> {
pub fn to_bytes(&self, buffer: &mut impl io::Write) -> io::Result<()> {
buffer.write_u64::<LittleEndian>(self.source_dimension() as u64)?;
buffer.write_u64::<LittleEndian>(self.target_dimension() as u64)?;
buffer.write_u64::<LittleEndian>(self.image_dimension() as u64)?;
Expand All @@ -61,7 +61,7 @@ impl QuasiInverse {
self.preimage.to_bytes(buffer)
}

pub fn from_bytes(p: ValidPrime, data: &mut impl Read) -> std::io::Result<Self> {
pub fn from_bytes(p: ValidPrime, data: &mut impl io::Read) -> io::Result<Self> {
let source_dim = data.read_u64::<LittleEndian>()? as usize;
let target_dim = data.read_u64::<LittleEndian>()? as usize;
let image_dim = data.read_u64::<LittleEndian>()? as usize;
Expand All @@ -79,10 +79,10 @@ impl QuasiInverse {
/// quasi-inverse row by row to minimize memory usage.
pub fn stream_quasi_inverse<T, S>(
p: ValidPrime,
data: &mut impl Read,
data: &mut impl io::Read,
results: &mut [T],
inputs: &[S],
) -> std::io::Result<()>
) -> io::Result<()>
where
for<'a> &'a mut T: Into<FpSliceMut<'a>>,
for<'a> &'a S: Into<FpSlice<'a>>,
Expand Down Expand Up @@ -173,7 +173,7 @@ mod tests {
let mut out0 = FpVector::new(p, 4);
let mut out1 = FpVector::new(p, 4);

let mut cursor = std::io::Cursor::new(Vec::<u8>::new());
let mut cursor = io::Cursor::new(Vec::<u8>::new());
qi.to_bytes(&mut cursor).unwrap();
cursor.set_position(0);

Expand Down
9 changes: 3 additions & 6 deletions ext/crates/fp/src/matrix/subspace.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use std::{
io::{Read, Write},
ops::Deref,
};
use std::{io, ops::Deref};

use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use itertools::Itertools;
Expand Down Expand Up @@ -60,7 +57,7 @@ impl Subspace {
ret
}

pub fn from_bytes(p: ValidPrime, data: &mut impl Read) -> std::io::Result<Self> {
pub fn from_bytes(p: ValidPrime, data: &mut impl io::Read) -> io::Result<Self> {
let rows = data.read_u64::<LittleEndian>()? as usize;
let ambient_dimension = data.read_u64::<LittleEndian>()? as usize;

Expand All @@ -71,7 +68,7 @@ impl Subspace {
Ok(Self { matrix })
}

pub fn to_bytes(&self, buffer: &mut impl Write) -> std::io::Result<()> {
pub fn to_bytes(&self, buffer: &mut impl io::Write) -> io::Result<()> {
buffer.write_u64::<LittleEndian>(self.matrix.rows() as u64)?;
buffer.write_u64::<LittleEndian>(self.ambient_dimension() as u64)?;

Expand Down
11 changes: 4 additions & 7 deletions ext/crates/fp/src/vector/fp_wrapper/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@
//! we can't simply define `type FpVector = FqVector<Fp<2>>` like we previously did: we need to use
//! a transparent wrapper.
use std::{
convert::TryInto,
io::{Read, Write},
};
use std::{convert::TryInto, io};

use itertools::Itertools;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
Expand Down Expand Up @@ -104,9 +101,9 @@ impl FpVector {
pub fn new<P: Prime>(p: P, len: usize) -> (from FqVector);
pub fn new_with_capacity<P: Prime>(p: P, len: usize, capacity: usize) -> (from FqVector);

pub fn update_from_bytes(&mut self, data: &mut impl Read) -> (std::io::Result<()>);
pub fn from_bytes<P: Prime>(p: P, len: usize, data: &mut impl Read) -> (from io FqVector);
pub fn to_bytes(&self, buffer: &mut impl Write) -> (std::io::Result<()>);
pub fn update_from_bytes(&mut self, data: &mut impl io::Read) -> (io::Result<()>);
pub fn from_bytes<P: Prime>(p: P, len: usize, data: &mut impl io::Read) -> (from io FqVector);
pub fn to_bytes(&self, buffer: &mut impl io::Write) -> (io::Result<()>);
}

pub fn from_slice<P: Prime>(p: P, slice: &[u32]) -> Self {
Expand Down
8 changes: 4 additions & 4 deletions ext/examples/bruner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
use std::{
fs::File,
io::{BufRead, BufReader},
io,
path::{Path, PathBuf},
str::FromStr,
sync::Arc,
Expand Down Expand Up @@ -47,7 +47,7 @@ type FreeModuleHomomorphism = FMH<FreeModule>;
type FiniteChainComplex = FCC<FreeModule, FreeModuleHomomorphism>;

/// Read the first non-empty line of `data` into `buf`. Returns whether a line is read
fn read_line(data: &mut impl BufRead, buf: &mut String) -> Result<bool> {
fn read_line(data: &mut impl io::BufRead, buf: &mut String) -> Result<bool> {
buf.clear();
while buf.is_empty() {
let num_bytes = data.read_line(buf)?;
Expand Down Expand Up @@ -108,7 +108,7 @@ fn get_algebra_element<'a>(
fn get_element(
a: &MilnorAlgebra,
m: &FreeModule,
input: &mut impl BufRead,
input: &mut impl io::BufRead,
) -> Result<Option<(i32, FpVector)>> {
let mut buf = String::new();
if !read_line(input, &mut buf)? {
Expand Down Expand Up @@ -188,7 +188,7 @@ fn read_bruner_resolution(data_dir: &Path, max_n: i32) -> Result<(u32, FiniteCha
let m = cc.module(s);
let d = cc.differential(s);

let mut f = BufReader::new(
let mut f = io::BufReader::new(
File::open(data_dir.join(format!("hDiff.{s}")))
.with_context(|| format!("Failed to read hDiff.{s}"))?,
);
Expand Down
Loading

0 comments on commit 3ab84b7

Please sign in to comment.