Skip to content

Commit 1e63fcf

Browse files
Return tail of slice from deserialize functions (#21)
* Return tail of slice from deserialize functions * Apply suggestions from code review Co-authored-by: Shunsuke Kanda <[email protected]> Co-authored-by: Shunsuke Kanda <[email protected]>
1 parent 987aeca commit 1e63fcf

File tree

2 files changed

+30
-16
lines changed

2 files changed

+30
-16
lines changed

src/charwise.rs

+15-8
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,10 @@ impl CharwiseDoubleArrayAhoCorasick {
779779
///
780780
/// * `source` - A source slice.
781781
///
782+
/// # Returns
783+
///
784+
/// A tuple of the automaton and the slice not used for the deserialization.
785+
///
782786
/// # Safety
783787
///
784788
/// The given data must be a correct automaton exported by
@@ -794,7 +798,7 @@ impl CharwiseDoubleArrayAhoCorasick {
794798
/// let pma = CharwiseDoubleArrayAhoCorasick::new(patterns).unwrap();
795799
/// let bytes = pma.serialize_to_vec();
796800
///
797-
/// let pma = unsafe {
801+
/// let (pma, _) = unsafe {
798802
/// CharwiseDoubleArrayAhoCorasick::deserialize_from_slice_unchecked(&bytes)
799803
/// };
800804
///
@@ -811,7 +815,7 @@ impl CharwiseDoubleArrayAhoCorasick {
811815
///
812816
/// assert_eq!(None, it.next());
813817
/// ```
814-
pub unsafe fn deserialize_from_slice_unchecked(mut source: &[u8]) -> Self {
818+
pub unsafe fn deserialize_from_slice_unchecked(mut source: &[u8]) -> (Self, &[u8]) {
815819
let states_len = u32::from_le_bytes(source[0..4].try_into().unwrap()) as usize;
816820
source = &source[4..];
817821
let mut states = Vec::with_capacity(states_len);
@@ -831,12 +835,15 @@ impl CharwiseDoubleArrayAhoCorasick {
831835
let num_states_array: [u8; 4] = source[1..5].try_into().unwrap();
832836
let num_states = u32::from_le_bytes(num_states_array) as usize;
833837

834-
Self {
835-
states,
836-
outputs,
837-
match_kind,
838-
num_states,
839-
}
838+
(
839+
Self {
840+
states,
841+
outputs,
842+
match_kind,
843+
num_states,
844+
},
845+
&source[5..],
846+
)
840847
}
841848

842849
/// # Safety

src/lib.rs

+15-8
Original file line numberDiff line numberDiff line change
@@ -1066,6 +1066,10 @@ impl DoubleArrayAhoCorasick {
10661066
///
10671067
/// * `source` - A source slice.
10681068
///
1069+
/// # Returns
1070+
///
1071+
/// A tuple of the automaton and the slice not used for the deserialization.
1072+
///
10691073
/// # Safety
10701074
///
10711075
/// The given data must be a correct automaton exported by
@@ -1081,7 +1085,7 @@ impl DoubleArrayAhoCorasick {
10811085
/// let pma = DoubleArrayAhoCorasick::new(patterns).unwrap();
10821086
/// let bytes = pma.serialize_to_vec();
10831087
///
1084-
/// let pma = unsafe {
1088+
/// let (pma, _) = unsafe {
10851089
/// DoubleArrayAhoCorasick::deserialize_from_slice_unchecked(&bytes)
10861090
/// };
10871091
///
@@ -1098,7 +1102,7 @@ impl DoubleArrayAhoCorasick {
10981102
///
10991103
/// assert_eq!(None, it.next());
11001104
/// ```
1101-
pub unsafe fn deserialize_from_slice_unchecked(mut source: &[u8]) -> Self {
1105+
pub unsafe fn deserialize_from_slice_unchecked(mut source: &[u8]) -> (Self, &[u8]) {
11021106
let states_len = u32::from_le_bytes(source[0..4].try_into().unwrap()) as usize;
11031107
source = &source[4..];
11041108
let mut states = Vec::with_capacity(states_len);
@@ -1118,12 +1122,15 @@ impl DoubleArrayAhoCorasick {
11181122
let num_states_array: [u8; 4] = source[1..5].try_into().unwrap();
11191123
let num_states = u32::from_le_bytes(num_states_array) as usize;
11201124

1121-
Self {
1122-
states,
1123-
outputs,
1124-
match_kind,
1125-
num_states,
1126-
}
1125+
(
1126+
Self {
1127+
states,
1128+
outputs,
1129+
match_kind,
1130+
num_states,
1131+
},
1132+
&source[5..],
1133+
)
11271134
}
11281135

11291136
/// # Safety

0 commit comments

Comments
 (0)