Skip to content

Commit ccb40e5

Browse files
committed
Use usize for SparseVector dim and indices
1 parent 5f3b7cf commit ccb40e5

File tree

4 files changed

+20
-20
lines changed

4 files changed

+20
-20
lines changed

src/diesel_ext/sparsevec.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ impl ToSql<SparseVectorType, Pg> for SparseVector {
1616
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
1717
let dim = self.dim;
1818
let nnz = self.indices.len();
19-
out.write_all(&dim.to_be_bytes())?;
19+
out.write_all(&i32::try_from(dim)?.to_be_bytes())?;
2020
out.write_all(&i32::try_from(nnz)?.to_be_bytes())?;
2121
out.write_all(&0_i32.to_be_bytes())?;
2222

2323
for v in &self.indices {
24-
out.write_all(&v.to_be_bytes())?;
24+
out.write_all(&i32::try_from(*v)?.to_be_bytes())?;
2525
}
2626

2727
for v in &self.values {

src/postgres_ext/sparsevec.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ impl ToSql for SparseVector {
1919
fn to_sql(&self, _ty: &Type, w: &mut BytesMut) -> Result<IsNull, Box<dyn Error + Sync + Send>> {
2020
let dim = self.dim;
2121
let nnz = self.indices.len();
22-
w.put_i32(dim);
22+
w.put_i32(dim.try_into()?);
2323
w.put_i32(nnz.try_into()?);
2424
w.put_i32(0);
2525

2626
for v in &self.indices {
27-
w.put_i32(*v);
27+
w.put_i32((*v).try_into()?);
2828
}
2929

3030
for v in &self.values {

src/sparsevec.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,21 @@ use diesel::{deserialize::FromSqlRow, expression::AsExpression};
99
#[cfg_attr(feature = "diesel", derive(FromSqlRow, AsExpression))]
1010
#[cfg_attr(feature = "diesel", diesel(sql_type = SparseVectorType))]
1111
pub struct SparseVector {
12-
pub(crate) dim: i32,
13-
pub(crate) indices: Vec<i32>,
12+
pub(crate) dim: usize,
13+
pub(crate) indices: Vec<usize>,
1414
pub(crate) values: Vec<f32>,
1515
}
1616

1717
impl SparseVector {
1818
/// Creates a sparse vector from a dense vector.
1919
pub fn from_dense(vec: &[f32]) -> SparseVector {
20-
let dim: i32 = vec.len().try_into().unwrap();
20+
let dim = vec.len();
2121
let mut indices = Vec::new();
2222
let mut values = Vec::new();
2323

2424
for (i, v) in vec.iter().enumerate() {
2525
if *v != 0.0 {
26-
indices.push(i.try_into().unwrap());
26+
indices.push(i);
2727
values.push(*v);
2828
}
2929
}
@@ -36,13 +36,13 @@ impl SparseVector {
3636
}
3737

3838
/// Creates a sparse vector from a map of non-zero elements.
39-
pub fn from_map<'a, I: IntoIterator<Item = (&'a i32, &'a f32)>>(
39+
pub fn from_map<'a, I: IntoIterator<Item = (&'a usize, &'a f32)>>(
4040
map: I,
41-
dim: i32,
41+
dim: usize,
4242
) -> SparseVector {
43-
let mut elements: Vec<(&i32, &f32)> = map.into_iter().filter(|v| *v.1 != 0.0).collect();
43+
let mut elements: Vec<(&usize, &f32)> = map.into_iter().filter(|v| *v.1 != 0.0).collect();
4444
elements.sort_by_key(|v| *v.0);
45-
let indices: Vec<i32> = elements.iter().map(|v| *v.0).collect();
45+
let indices: Vec<usize> = elements.iter().map(|v| *v.0).collect();
4646
let values: Vec<f32> = elements.iter().map(|v| *v.1).collect();
4747

4848
SparseVector {
@@ -53,12 +53,12 @@ impl SparseVector {
5353
}
5454

5555
/// Returns the number of dimensions.
56-
pub fn dimensions(&self) -> i32 {
56+
pub fn dimensions(&self) -> usize {
5757
self.dim
5858
}
5959

6060
/// Returns the non-zero indices.
61-
pub fn indices(&self) -> &[i32] {
61+
pub fn indices(&self) -> &[usize] {
6262
&self.indices
6363
}
6464

@@ -69,9 +69,9 @@ impl SparseVector {
6969

7070
/// Returns the sparse vector as a `Vec<f32>`.
7171
pub fn to_vec(&self) -> Vec<f32> {
72-
let mut vec = vec![0.0; self.dim.try_into().unwrap()];
72+
let mut vec = vec![0.0; self.dim];
7373
for (i, v) in self.indices.iter().zip(&self.values) {
74-
vec[usize::try_from(*i).unwrap()] = *v;
74+
vec[*i] = *v;
7575
}
7676
vec
7777
}
@@ -80,7 +80,7 @@ impl SparseVector {
8080
pub(crate) fn from_sql(
8181
buf: &[u8],
8282
) -> Result<SparseVector, Box<dyn std::error::Error + Sync + Send>> {
83-
let dim = i32::from_be_bytes(buf[0..4].try_into()?);
83+
let dim = i32::from_be_bytes(buf[0..4].try_into()?).try_into()?;
8484
let nnz = i32::from_be_bytes(buf[4..8].try_into()?).try_into()?;
8585
let unused = i32::from_be_bytes(buf[8..12].try_into()?);
8686
if unused != 0 {
@@ -90,7 +90,7 @@ impl SparseVector {
9090
let mut indices = Vec::with_capacity(nnz);
9191
for i in 0..nnz {
9292
let s = 12 + 4 * i;
93-
indices.push(i32::from_be_bytes(buf[s..s + 4].try_into()?));
93+
indices.push(i32::from_be_bytes(buf[s..s + 4].try_into()?).try_into()?);
9494
}
9595

9696
let mut values = Vec::with_capacity(nnz);

src/sqlx_ext/sparsevec.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ impl Encode<'_, Postgres> for SparseVector {
1616
fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> Result<IsNull, BoxDynError> {
1717
let dim = self.dim;
1818
let nnz = self.indices.len();
19-
buf.extend(dim.to_be_bytes());
19+
buf.extend(&i32::try_from(dim)?.to_be_bytes());
2020
buf.extend(&i32::try_from(nnz)?.to_be_bytes());
2121
buf.extend(&0_i32.to_be_bytes());
2222

2323
for v in &self.indices {
24-
buf.extend(&v.to_be_bytes());
24+
buf.extend(&i32::try_from(*v)?.to_be_bytes());
2525
}
2626

2727
for v in &self.values {

0 commit comments

Comments
 (0)