Skip to content

Commit

Permalink
Added as_slice method - closes #10
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Oct 19, 2023
1 parent 948e6a0 commit 5976df8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.3.1 (unreleased)

- Added `as_slice` method

## 0.3.0 (2023-10-17)

- Added `serde` feature
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,12 @@ Convert a vector to a `Vec<f32>`
let f32_vec: Vec<f32> = vec.into();
```

Get a slice [unreleased]

```rust
let slice = vec.as_slice();
```

## History

View the [changelog](https://github.com/pgvector/pgvector-rust/blob/master/CHANGELOG.md)
Expand Down
11 changes: 11 additions & 0 deletions src/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ impl Vector {
self.0.clone()
}

/// Returns the vector as a slice.
pub fn as_slice(&self) -> &[f32] {
self.0.as_slice()
}

#[cfg(any(feature = "postgres", feature = "sqlx", feature = "diesel"))]
pub(crate) fn from_sql(buf: &[u8]) -> Result<Vector, Box<dyn std::error::Error + Sync + Send>> {
let dim = u16::from_be_bytes(buf[0..2].try_into()?) as usize;
Expand Down Expand Up @@ -75,6 +80,12 @@ mod tests {
assert_eq!(vec.to_vec(), vec![1.0, 2.0, 3.0]);
}

#[test]
fn test_as_slice() {
let vec = Vector::from(vec![1.0, 2.0, 3.0]);
assert_eq!(vec.as_slice(), &[1.0, 2.0, 3.0]);
}

#[cfg(feature = "serde")]
#[test]
fn test_serialize() {
Expand Down

0 comments on commit 5976df8

Please sign in to comment.