2021-11-08 18:31:27 +01:00
|
|
|
use std::borrow::Cow;
|
2021-12-06 15:45:41 +01:00
|
|
|
use std::cmp::Reverse;
|
2022-07-06 03:08:19 +02:00
|
|
|
use std::collections::HashSet;
|
2021-11-08 18:31:27 +01:00
|
|
|
use std::fs::create_dir_all;
|
|
|
|
use std::path::Path;
|
2023-11-22 18:21:19 +01:00
|
|
|
use std::result::Result as StdResult;
|
2021-11-08 18:31:27 +01:00
|
|
|
use std::str;
|
2023-02-13 17:26:34 +01:00
|
|
|
use std::str::FromStr;
|
2021-12-15 17:55:39 +01:00
|
|
|
use std::sync::Arc;
|
2021-11-08 18:31:27 +01:00
|
|
|
|
2022-06-08 14:04:45 +02:00
|
|
|
use hmac::{Hmac, Mac};
|
2023-11-22 18:21:19 +01:00
|
|
|
use meilisearch_types::heed::BoxedError;
|
2023-01-25 16:12:40 +01:00
|
|
|
use meilisearch_types::index_uid_pattern::IndexUidPattern;
|
2022-10-12 16:10:28 +02:00
|
|
|
use meilisearch_types::keys::KeyId;
|
2022-10-17 15:11:35 +02:00
|
|
|
use meilisearch_types::milli;
|
2023-11-27 11:52:22 +01:00
|
|
|
use meilisearch_types::milli::heed::types::{Bytes, DecodeIgnore, SerdeJson};
|
2022-10-17 10:59:24 +02:00
|
|
|
use meilisearch_types::milli::heed::{Database, Env, EnvOpenOptions, RwTxn};
|
2022-07-05 14:13:20 +02:00
|
|
|
use sha2::Sha256;
|
2023-11-28 14:21:49 +01:00
|
|
|
use thiserror::Error;
|
2022-02-14 15:32:41 +01:00
|
|
|
use time::OffsetDateTime;
|
2022-07-05 14:13:20 +02:00
|
|
|
use uuid::fmt::Hyphenated;
|
2022-05-25 10:32:47 +02:00
|
|
|
use uuid::Uuid;
|
2021-11-08 18:31:27 +01:00
|
|
|
|
2023-02-13 17:26:34 +01:00
|
|
|
use super::error::{AuthControllerError, Result};
|
2021-11-08 18:31:27 +01:00
|
|
|
use super::{Action, Key};
|
|
|
|
|
|
|
|
const AUTH_STORE_SIZE: usize = 1_073_741_824; //1GiB
|
|
|
|
const AUTH_DB_PATH: &str = "auth";
|
|
|
|
const KEY_DB_NAME: &str = "api-keys";
|
|
|
|
const KEY_ID_ACTION_INDEX_EXPIRATION_DB_NAME: &str = "keyid-action-index-expiration";
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct HeedAuthStore {
|
2021-12-15 17:55:39 +01:00
|
|
|
env: Arc<Env>,
|
2023-11-27 11:52:22 +01:00
|
|
|
keys: Database<Bytes, SerdeJson<Key>>,
|
2022-02-14 15:32:41 +01:00
|
|
|
action_keyid_index_expiration: Database<KeyIdActionCodec, SerdeJson<Option<OffsetDateTime>>>,
|
2022-02-09 13:55:36 +01:00
|
|
|
should_close_on_drop: bool,
|
2021-11-08 18:31:27 +01:00
|
|
|
}
|
|
|
|
|
2021-12-15 17:55:39 +01:00
|
|
|
impl Drop for HeedAuthStore {
|
|
|
|
fn drop(&mut self) {
|
2022-02-09 13:55:36 +01:00
|
|
|
if self.should_close_on_drop && Arc::strong_count(&self.env) == 1 {
|
2021-12-15 17:55:39 +01:00
|
|
|
self.env.as_ref().clone().prepare_for_closing();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-16 13:45:58 +01:00
|
|
|
pub fn open_auth_store_env(path: &Path) -> milli::heed::Result<milli::heed::Env> {
|
2022-02-22 18:16:02 +01:00
|
|
|
let mut options = EnvOpenOptions::new();
|
|
|
|
options.map_size(AUTH_STORE_SIZE); // 1GB
|
|
|
|
options.max_dbs(2);
|
2024-05-16 16:10:55 +02:00
|
|
|
unsafe { options.open(path) }
|
2022-02-22 18:16:02 +01:00
|
|
|
}
|
|
|
|
|
2021-11-08 18:31:27 +01:00
|
|
|
impl HeedAuthStore {
|
|
|
|
pub fn new(path: impl AsRef<Path>) -> Result<Self> {
|
|
|
|
let path = path.as_ref().join(AUTH_DB_PATH);
|
|
|
|
create_dir_all(&path)?;
|
2022-02-22 18:16:02 +01:00
|
|
|
let env = Arc::new(open_auth_store_env(path.as_ref())?);
|
2023-05-15 10:15:33 +02:00
|
|
|
let mut wtxn = env.write_txn()?;
|
|
|
|
let keys = env.create_database(&mut wtxn, Some(KEY_DB_NAME))?;
|
2021-11-08 18:31:27 +01:00
|
|
|
let action_keyid_index_expiration =
|
2023-05-15 10:15:33 +02:00
|
|
|
env.create_database(&mut wtxn, Some(KEY_ID_ACTION_INDEX_EXPIRATION_DB_NAME))?;
|
|
|
|
wtxn.commit()?;
|
2022-10-20 18:00:07 +02:00
|
|
|
Ok(Self { env, keys, action_keyid_index_expiration, should_close_on_drop: true })
|
2021-11-08 18:31:27 +01:00
|
|
|
}
|
|
|
|
|
2023-04-06 13:38:47 +02:00
|
|
|
/// Return `Ok(())` if the auth store is able to access one of its database.
|
|
|
|
pub fn health(&self) -> Result<()> {
|
|
|
|
let rtxn = self.env.read_txn()?;
|
|
|
|
self.keys.first(&rtxn)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-01-24 16:17:23 +01:00
|
|
|
/// Return the size in bytes of database
|
|
|
|
pub fn size(&self) -> Result<u64> {
|
|
|
|
Ok(self.env.real_disk_size()?)
|
|
|
|
}
|
|
|
|
|
2023-05-25 18:30:30 +02:00
|
|
|
/// Return the number of bytes actually used in the database
|
|
|
|
pub fn used_size(&self) -> Result<u64> {
|
|
|
|
Ok(self.env.non_free_pages_size()?)
|
|
|
|
}
|
|
|
|
|
2022-02-09 13:55:36 +01:00
|
|
|
pub fn set_drop_on_close(&mut self, v: bool) {
|
|
|
|
self.should_close_on_drop = v;
|
|
|
|
}
|
|
|
|
|
2021-11-08 18:31:27 +01:00
|
|
|
pub fn is_empty(&self) -> Result<bool> {
|
|
|
|
let rtxn = self.env.read_txn()?;
|
|
|
|
|
|
|
|
Ok(self.keys.len(&rtxn)? == 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn put_api_key(&self, key: Key) -> Result<Key> {
|
2022-05-25 10:32:47 +02:00
|
|
|
let uid = key.uid;
|
2021-11-08 18:31:27 +01:00
|
|
|
let mut wtxn = self.env.write_txn()?;
|
|
|
|
|
2022-05-25 10:32:47 +02:00
|
|
|
self.keys.put(&mut wtxn, uid.as_bytes(), &key)?;
|
|
|
|
|
2021-11-08 18:31:27 +01:00
|
|
|
// delete key from inverted database before refilling it.
|
2022-05-25 10:32:47 +02:00
|
|
|
self.delete_key_from_inverted_db(&mut wtxn, &uid)?;
|
2021-11-08 18:31:27 +01:00
|
|
|
// create inverted database.
|
|
|
|
let db = self.action_keyid_index_expiration;
|
|
|
|
|
2022-07-06 03:06:58 +02:00
|
|
|
let mut actions = HashSet::new();
|
|
|
|
for action in &key.actions {
|
|
|
|
match action {
|
2022-10-04 12:04:16 +02:00
|
|
|
Action::All => actions.extend(enum_iterator::all::<Action>()),
|
2022-07-06 03:08:19 +02:00
|
|
|
Action::DocumentsAll => {
|
|
|
|
actions.extend(
|
2022-10-20 18:00:07 +02:00
|
|
|
[Action::DocumentsGet, Action::DocumentsDelete, Action::DocumentsAdd]
|
|
|
|
.iter(),
|
2022-07-06 03:08:19 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
Action::IndexesAll => {
|
|
|
|
actions.extend(
|
|
|
|
[
|
|
|
|
Action::IndexesAdd,
|
|
|
|
Action::IndexesDelete,
|
2022-07-07 04:24:46 +02:00
|
|
|
Action::IndexesGet,
|
2022-07-06 03:08:19 +02:00
|
|
|
Action::IndexesUpdate,
|
2022-10-22 12:52:36 +02:00
|
|
|
Action::IndexesSwap,
|
2022-07-06 03:08:19 +02:00
|
|
|
]
|
|
|
|
.iter(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
Action::SettingsAll => {
|
|
|
|
actions.extend([Action::SettingsGet, Action::SettingsUpdate].iter());
|
|
|
|
}
|
|
|
|
Action::DumpsAll => {
|
|
|
|
actions.insert(Action::DumpsCreate);
|
|
|
|
}
|
2023-09-11 11:00:05 +02:00
|
|
|
Action::SnapshotsAll => {
|
|
|
|
actions.insert(Action::SnapshotsCreate);
|
|
|
|
}
|
2022-07-06 03:08:19 +02:00
|
|
|
Action::TasksAll => {
|
2022-10-22 12:52:36 +02:00
|
|
|
actions.extend([Action::TasksGet, Action::TasksDelete, Action::TasksCancel]);
|
2022-07-06 03:08:19 +02:00
|
|
|
}
|
|
|
|
Action::StatsAll => {
|
|
|
|
actions.insert(Action::StatsGet);
|
|
|
|
}
|
2022-08-24 17:03:30 +02:00
|
|
|
Action::MetricsAll => {
|
|
|
|
actions.insert(Action::MetricsGet);
|
|
|
|
}
|
2022-07-06 03:08:19 +02:00
|
|
|
other => {
|
|
|
|
actions.insert(*other);
|
|
|
|
}
|
2022-07-06 03:06:58 +02:00
|
|
|
}
|
|
|
|
}
|
2021-11-08 18:31:27 +01:00
|
|
|
|
2023-02-01 18:21:45 +01:00
|
|
|
let no_index_restriction = key.indexes.iter().any(|p| p.matches_all());
|
2021-11-08 18:31:27 +01:00
|
|
|
for action in actions {
|
|
|
|
if no_index_restriction {
|
|
|
|
// If there is no index restriction we put None.
|
2022-05-25 10:32:47 +02:00
|
|
|
db.put(&mut wtxn, &(&uid, &action, None), &key.expires_at)?;
|
2021-11-08 18:31:27 +01:00
|
|
|
} else {
|
|
|
|
// else we create a key for each index.
|
|
|
|
for index in key.indexes.iter() {
|
|
|
|
db.put(
|
|
|
|
&mut wtxn,
|
2023-01-16 16:59:26 +01:00
|
|
|
&(&uid, &action, Some(index.to_string().as_bytes())),
|
2021-11-08 18:31:27 +01:00
|
|
|
&key.expires_at,
|
|
|
|
)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
wtxn.commit()?;
|
|
|
|
|
|
|
|
Ok(key)
|
|
|
|
}
|
|
|
|
|
2022-05-25 10:32:47 +02:00
|
|
|
pub fn get_api_key(&self, uid: Uuid) -> Result<Option<Key>> {
|
2021-11-08 18:31:27 +01:00
|
|
|
let rtxn = self.env.read_txn()?;
|
2022-05-25 10:32:47 +02:00
|
|
|
self.keys.get(&rtxn, uid.as_bytes()).map_err(|e| e.into())
|
|
|
|
}
|
|
|
|
|
2022-06-01 18:06:20 +02:00
|
|
|
pub fn get_uid_from_encoded_key(
|
|
|
|
&self,
|
|
|
|
encoded_key: &[u8],
|
|
|
|
master_key: &[u8],
|
|
|
|
) -> Result<Option<Uuid>> {
|
2022-05-25 10:32:47 +02:00
|
|
|
let rtxn = self.env.read_txn()?;
|
|
|
|
let uid = self
|
|
|
|
.keys
|
|
|
|
.remap_data_type::<DecodeIgnore>()
|
|
|
|
.iter(&rtxn)?
|
|
|
|
.filter_map(|res| match res {
|
2022-07-05 14:13:20 +02:00
|
|
|
Ok((uid, _)) => {
|
2022-05-25 10:32:47 +02:00
|
|
|
let (uid, _) = try_split_array_at(uid)?;
|
2022-07-05 14:13:20 +02:00
|
|
|
let uid = Uuid::from_bytes(*uid);
|
|
|
|
if generate_key_as_hexa(uid, master_key).as_bytes() == encoded_key {
|
|
|
|
Some(uid)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2022-05-25 10:32:47 +02:00
|
|
|
}
|
2022-07-05 14:13:20 +02:00
|
|
|
Err(_) => None,
|
2022-05-25 10:32:47 +02:00
|
|
|
})
|
|
|
|
.next();
|
|
|
|
|
|
|
|
Ok(uid)
|
2021-11-08 18:31:27 +01:00
|
|
|
}
|
|
|
|
|
2022-05-25 10:32:47 +02:00
|
|
|
pub fn delete_api_key(&self, uid: Uuid) -> Result<bool> {
|
2021-11-08 18:31:27 +01:00
|
|
|
let mut wtxn = self.env.write_txn()?;
|
2022-05-25 10:32:47 +02:00
|
|
|
let existing = self.keys.delete(&mut wtxn, uid.as_bytes())?;
|
|
|
|
self.delete_key_from_inverted_db(&mut wtxn, &uid)?;
|
2021-11-08 18:31:27 +01:00
|
|
|
wtxn.commit()?;
|
|
|
|
|
|
|
|
Ok(existing)
|
|
|
|
}
|
|
|
|
|
2022-10-16 01:39:01 +02:00
|
|
|
pub fn delete_all_keys(&self) -> Result<()> {
|
|
|
|
let mut wtxn = self.env.write_txn()?;
|
|
|
|
self.keys.clear(&mut wtxn)?;
|
|
|
|
wtxn.commit()?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-11-08 18:31:27 +01:00
|
|
|
pub fn list_api_keys(&self) -> Result<Vec<Key>> {
|
|
|
|
let mut list = Vec::new();
|
|
|
|
let rtxn = self.env.read_txn()?;
|
|
|
|
for result in self.keys.remap_key_type::<DecodeIgnore>().iter(&rtxn)? {
|
|
|
|
let (_, content) = result?;
|
|
|
|
list.push(content);
|
|
|
|
}
|
2021-12-06 15:45:41 +01:00
|
|
|
list.sort_unstable_by_key(|k| Reverse(k.created_at));
|
2021-11-08 18:31:27 +01:00
|
|
|
Ok(list)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_expiration_date(
|
|
|
|
&self,
|
2022-05-25 10:32:47 +02:00
|
|
|
uid: Uuid,
|
2021-11-08 18:31:27 +01:00
|
|
|
action: Action,
|
2023-01-25 16:12:40 +01:00
|
|
|
index: Option<&str>,
|
2022-02-14 15:32:41 +01:00
|
|
|
) -> Result<Option<Option<OffsetDateTime>>> {
|
2021-11-08 18:31:27 +01:00
|
|
|
let rtxn = self.env.read_txn()?;
|
2023-01-25 16:12:40 +01:00
|
|
|
let tuple = (&uid, &action, index.map(|s| s.as_bytes()));
|
|
|
|
match self.action_keyid_index_expiration.get(&rtxn, &tuple)? {
|
|
|
|
Some(expiration) => Ok(Some(expiration)),
|
|
|
|
None => {
|
|
|
|
let tuple = (&uid, &action, None);
|
|
|
|
for result in self.action_keyid_index_expiration.prefix_iter(&rtxn, &tuple)? {
|
|
|
|
let ((_, _, index_uid_pattern), expiration) = result?;
|
|
|
|
if let Some((pattern, index)) = index_uid_pattern.zip(index) {
|
2023-02-13 17:26:34 +01:00
|
|
|
let index_uid_pattern = str::from_utf8(pattern)?;
|
|
|
|
let pattern = IndexUidPattern::from_str(index_uid_pattern)
|
|
|
|
.map_err(|e| AuthControllerError::Internal(Box::new(e)))?;
|
2023-01-25 16:12:40 +01:00
|
|
|
if pattern.matches_str(index) {
|
|
|
|
return Ok(Some(expiration));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
}
|
2021-11-08 18:31:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn prefix_first_expiration_date(
|
|
|
|
&self,
|
2022-05-25 10:32:47 +02:00
|
|
|
uid: Uuid,
|
2021-11-08 18:31:27 +01:00
|
|
|
action: Action,
|
2022-02-14 15:32:41 +01:00
|
|
|
) -> Result<Option<Option<OffsetDateTime>>> {
|
2021-11-08 18:31:27 +01:00
|
|
|
let rtxn = self.env.read_txn()?;
|
2022-05-25 10:32:47 +02:00
|
|
|
let tuple = (&uid, &action, None);
|
|
|
|
let exp = self
|
|
|
|
.action_keyid_index_expiration
|
|
|
|
.prefix_iter(&rtxn, &tuple)?
|
|
|
|
.next()
|
|
|
|
.transpose()?
|
|
|
|
.map(|(_, expiration)| expiration);
|
2021-11-08 18:31:27 +01:00
|
|
|
|
2022-05-25 10:32:47 +02:00
|
|
|
Ok(exp)
|
2022-01-12 15:35:33 +01:00
|
|
|
}
|
|
|
|
|
2021-11-08 18:31:27 +01:00
|
|
|
fn delete_key_from_inverted_db(&self, wtxn: &mut RwTxn, key: &KeyId) -> Result<()> {
|
|
|
|
let mut iter = self
|
|
|
|
.action_keyid_index_expiration
|
2023-11-27 11:52:22 +01:00
|
|
|
.remap_types::<Bytes, DecodeIgnore>()
|
2022-05-25 10:32:47 +02:00
|
|
|
.prefix_iter_mut(wtxn, key.as_bytes())?;
|
2021-11-08 18:31:27 +01:00
|
|
|
while iter.next().transpose()?.is_some() {
|
|
|
|
// safety: we don't keep references from inside the LMDB database.
|
|
|
|
unsafe { iter.del_current()? };
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Codec allowing to retrieve the expiration date of an action,
|
2022-06-05 03:42:53 +02:00
|
|
|
/// optionally on a specific index, for a given key.
|
2021-11-08 18:31:27 +01:00
|
|
|
pub struct KeyIdActionCodec;
|
|
|
|
|
2022-03-16 13:45:58 +01:00
|
|
|
impl<'a> milli::heed::BytesDecode<'a> for KeyIdActionCodec {
|
2021-11-08 18:31:27 +01:00
|
|
|
type DItem = (KeyId, Action, Option<&'a [u8]>);
|
|
|
|
|
2023-11-22 18:21:19 +01:00
|
|
|
fn bytes_decode(bytes: &'a [u8]) -> StdResult<Self::DItem, BoxedError> {
|
2023-11-28 14:21:49 +01:00
|
|
|
let (key_id_bytes, action_bytes) = try_split_array_at(bytes).ok_or(SliceTooShortError)?;
|
|
|
|
let (&action_byte, index) =
|
|
|
|
match try_split_array_at(action_bytes).ok_or(SliceTooShortError)? {
|
|
|
|
([action], []) => (action, None),
|
|
|
|
([action], index) => (action, Some(index)),
|
|
|
|
};
|
2022-05-25 10:32:47 +02:00
|
|
|
let key_id = Uuid::from_bytes(*key_id_bytes);
|
2023-11-28 14:21:49 +01:00
|
|
|
let action = Action::from_repr(action_byte).ok_or(InvalidActionError { action_byte })?;
|
2021-11-08 18:31:27 +01:00
|
|
|
|
2023-11-22 18:21:19 +01:00
|
|
|
Ok((key_id, action, index))
|
2021-11-08 18:31:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-16 13:45:58 +01:00
|
|
|
impl<'a> milli::heed::BytesEncode<'a> for KeyIdActionCodec {
|
2021-11-08 18:31:27 +01:00
|
|
|
type EItem = (&'a KeyId, &'a Action, Option<&'a [u8]>);
|
|
|
|
|
2023-11-22 18:21:19 +01:00
|
|
|
fn bytes_encode((key_id, action, index): &Self::EItem) -> StdResult<Cow<[u8]>, BoxedError> {
|
2021-11-08 18:31:27 +01:00
|
|
|
let mut bytes = Vec::new();
|
|
|
|
|
2022-05-25 10:32:47 +02:00
|
|
|
bytes.extend_from_slice(key_id.as_bytes());
|
2021-11-08 18:31:27 +01:00
|
|
|
let action_bytes = u8::to_be_bytes(action.repr());
|
|
|
|
bytes.extend_from_slice(&action_bytes);
|
|
|
|
if let Some(index) = index {
|
|
|
|
bytes.extend_from_slice(index);
|
|
|
|
}
|
|
|
|
|
2023-11-22 18:21:19 +01:00
|
|
|
Ok(Cow::Owned(bytes))
|
2021-11-08 18:31:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-28 14:21:49 +01:00
|
|
|
#[derive(Error, Debug)]
|
|
|
|
#[error("the slice is too short")]
|
|
|
|
pub struct SliceTooShortError;
|
|
|
|
|
|
|
|
#[derive(Error, Debug)]
|
|
|
|
#[error("cannot construct a valid Action from {action_byte}")]
|
|
|
|
pub struct InvalidActionError {
|
|
|
|
pub action_byte: u8,
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:13:20 +02:00
|
|
|
pub fn generate_key_as_hexa(uid: Uuid, master_key: &[u8]) -> String {
|
|
|
|
// format uid as hyphenated allowing user to generate their own keys.
|
|
|
|
let mut uid_buffer = [0; Hyphenated::LENGTH];
|
|
|
|
let uid = uid.hyphenated().encode_lower(&mut uid_buffer);
|
|
|
|
|
|
|
|
// new_from_slice function never fail.
|
|
|
|
let mut mac = Hmac::<Sha256>::new_from_slice(master_key).unwrap();
|
|
|
|
mac.update(uid.as_bytes());
|
2022-06-08 14:04:45 +02:00
|
|
|
|
|
|
|
let result = mac.finalize();
|
2022-07-05 14:13:20 +02:00
|
|
|
format!("{:x}", result.into_bytes())
|
2022-05-25 10:32:47 +02:00
|
|
|
}
|
|
|
|
|
2021-11-08 18:31:27 +01:00
|
|
|
/// Divides one slice into two at an index, returns `None` if mid is out of bounds.
|
|
|
|
pub fn try_split_at<T>(slice: &[T], mid: usize) -> Option<(&[T], &[T])> {
|
|
|
|
if mid <= slice.len() {
|
|
|
|
Some(slice.split_at(mid))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Divides one slice into an array and the tail at an index,
|
|
|
|
/// returns `None` if `N` is out of bounds.
|
|
|
|
pub fn try_split_array_at<T, const N: usize>(slice: &[T]) -> Option<(&[T; N], &[T])>
|
|
|
|
where
|
|
|
|
[T; N]: for<'a> TryFrom<&'a [T]>,
|
|
|
|
{
|
|
|
|
let (head, tail) = try_split_at(slice, N)?;
|
|
|
|
let head = head.try_into().ok()?;
|
|
|
|
Some((head, tail))
|
|
|
|
}
|