Skip to content

Commit

Permalink
chore: rename native_model feature
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent-herlemont committed Nov 1, 2023
1 parent ab68293 commit ca9d6cc
Show file tree
Hide file tree
Showing 26 changed files with 46 additions and 46 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ bincode = { version = "2.0.0-rc.3", features = ["serde"] }

# Optional tokio support
tokio = { version = "1", features = ["sync"], optional = true }
native_model = { path = "../native_model", optional = true }
native_model_dep = { package = "native_model", path = "../native_model", optional = true }

[dev-dependencies]
assert_fs = "1.0"
Expand All @@ -36,7 +36,7 @@ tokio = { version = "1.33", features = ["test-util","macros"] }

[features]
default = []
use_native_model = ["native_model", "struct_db_macro/use_native_model"]
native_model = ["native_model_dep", "struct_db_macro/native_model"]

[build-dependencies]
skeptic = "0.13"
4 changes: 2 additions & 2 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl Db {
let mut primary_table_definition: PrimaryTableDefinition =
(schema.clone(), main_table_definition).into();

#[cfg(feature = "use_native_model")]
#[cfg(feature = "native_model")]
{
primary_table_definition.native_model_id = T::native_model_id();
primary_table_definition.native_model_version = T::native_model_version();
Expand Down Expand Up @@ -114,7 +114,7 @@ impl Db {
Ok(())
}

#[cfg(feature = "use_native_model")]
#[cfg(feature = "native_model")]
pub fn migrate<T: SDBItem + Debug>(&mut self) -> Result<()> {
use redb::ReadableTable;

Expand Down
4 changes: 2 additions & 2 deletions src/item.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[cfg(not(feature = "use_native_model"))]
#[cfg(not(feature = "native_model"))]
pub trait SDBItem: Sized {
fn struct_db_schema() -> crate::Schema;
fn struct_db_primary_key(&self) -> Vec<u8>;
Expand All @@ -17,7 +17,7 @@ pub trait SDBItem: Sized {
}
}

#[cfg(feature = "use_native_model")]
#[cfg(feature = "native_model")]
pub trait SDBItem: Sized + native_model::Model {
fn struct_db_schema() -> crate::Schema;
fn struct_db_primary_key(&self) -> Vec<u8>;
Expand Down
11 changes: 5 additions & 6 deletions src/serialization.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@

#[cfg(not(feature = "use_native_model"))]
#[cfg(not(feature = "native_model"))]
pub fn bincode_encode_to_vec<T>(value: &T) -> Option<Vec<u8>>
where
T: serde::Serialize,
{
bincode::serde::encode_to_vec(value, bincode::config::standard()).ok()
}
#[cfg(feature = "use_native_model")]
#[cfg(feature = "native_model")]
pub fn bincode_encode_to_vec<T>(value: &T) -> Option<Vec<u8>>
where
T: serde::Serialize + native_model::Model,
{
native_model::encode(value).ok()
}

#[cfg(not(feature = "use_native_model"))]
#[cfg(not(feature = "native_model"))]
pub fn bincode_decode_from_slice<T>(slice: &[u8]) -> Option<(T, usize)>
where
T: serde::de::DeserializeOwned,
{
bincode::serde::decode_from_slice(slice, bincode::config::standard()).ok()
bincode::serde::decode_from_slice(slice, bincode::config::standard()).ok()
}

#[cfg(feature = "use_native_model")]
#[cfg(feature = "native_model")]
pub fn bincode_decode_from_slice<T>(slice: &[u8]) -> Option<(T, usize)>
where
T: serde::de::DeserializeOwned + native_model::Model,
Expand Down
12 changes: 6 additions & 6 deletions src/table_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ use redb::TableHandle;
use std::collections::HashMap;
use std::fmt::Debug;

#[cfg(not(feature = "use_native_model"))]
#[cfg(not(feature = "native_model"))]
pub(crate) struct PrimaryTableDefinition {
pub(crate) schema: crate::Schema,
pub(crate) redb: redb::TableDefinition<'static, &'static [u8], &'static [u8]>,
pub(crate) secondary_tables: HashMap<&'static str, SecondaryTableDefinition>,
}

#[cfg(feature = "use_native_model")]
#[cfg(feature = "native_model")]
pub(crate) struct PrimaryTableDefinition {
pub(crate) schema: crate::Schema,
pub(crate) redb: redb::TableDefinition<'static, &'static [u8], &'static [u8]>,
Expand All @@ -29,7 +29,7 @@ impl
redb::TableDefinition<'static, &'static [u8], &'static [u8]>,
)> for PrimaryTableDefinition
{
#[cfg(not(feature = "use_native_model"))]
#[cfg(not(feature = "native_model"))]
fn from(
input: (
schema::Schema,
Expand All @@ -44,7 +44,7 @@ impl
}
}

#[cfg(feature = "use_native_model")]
#[cfg(feature = "native_model")]
fn from(
input: (
schema::Schema,
Expand All @@ -63,7 +63,7 @@ impl
}
}

#[cfg(feature = "use_native_model")]
#[cfg(feature = "native_model")]
impl Debug for PrimaryTableDefinition {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("TableDefinition")
Expand All @@ -75,7 +75,7 @@ impl Debug for PrimaryTableDefinition {
}
}

#[cfg(not(feature = "use_native_model"))]
#[cfg(not(feature = "native_model"))]
impl Debug for PrimaryTableDefinition {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("TableDefinition")
Expand Down
2 changes: 1 addition & 1 deletion struct_db_macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ quote = "1.0"

[features]
default = []
use_native_model = []
native_model = []
4 changes: 2 additions & 2 deletions struct_db_macro/src/struct_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ pub fn struct_db(args: TokenStream, input: TokenStream) -> TokenStream {
}
});

#[cfg(feature = "use_native_model")]
#[cfg(feature = "native_model")]
let is_native_model_exists = quote! {
true
};
#[cfg(not(feature = "use_native_model"))]
#[cfg(not(feature = "native_model"))]
let is_native_model_exists = quote! {
false
};
Expand Down
2 changes: 1 addition & 1 deletion tests/00_bincode.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![cfg(not(feature = "use_native_model"))]
#![cfg(not(feature = "native_model"))]
mod tests;

use serde::{Deserialize, Serialize};
Expand Down
2 changes: 1 addition & 1 deletion tests/01_fn_primary_key.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![cfg(not(feature = "use_native_model"))]
#![cfg(not(feature = "native_model"))]
mod tests;

use serde::{Deserialize, Serialize};
Expand Down
2 changes: 1 addition & 1 deletion tests/02_simple_insert.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![cfg(not(feature = "use_native_model"))]
#![cfg(not(feature = "native_model"))]
mod tests;

use serde::{Deserialize, Serialize};
Expand Down
2 changes: 1 addition & 1 deletion tests/03_simple_insert_get.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![cfg(not(feature = "use_native_model"))]
#![cfg(not(feature = "native_model"))]
mod tests;

use serde::{Deserialize, Serialize};
Expand Down
2 changes: 1 addition & 1 deletion tests/04_simple_len.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![cfg(not(feature = "use_native_model"))]
#![cfg(not(feature = "native_model"))]
mod tests;

use serde::{Deserialize, Serialize};
Expand Down
2 changes: 1 addition & 1 deletion tests/05_update.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![cfg(not(feature = "use_native_model"))]
#![cfg(not(feature = "native_model"))]
mod tests;

use serde::{Deserialize, Serialize};
Expand Down
4 changes: 2 additions & 2 deletions tests/06_transaction.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#![cfg(not(feature = "use_native_model"))]
#![cfg(not(feature = "native_model"))]
mod tests;

use std::panic::AssertUnwindSafe;
use serde::{Deserialize, Serialize};
use std::panic::AssertUnwindSafe;
use struct_db::*;

#[derive(Serialize, Deserialize, Eq, PartialEq, Debug, Clone)]
Expand Down
2 changes: 1 addition & 1 deletion tests/07_simple_multithreads.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![cfg(not(feature = "use_native_model"))]
#![cfg(not(feature = "native_model"))]
mod tests;

use serde::{Deserialize, Serialize};
Expand Down
2 changes: 1 addition & 1 deletion tests/08_fn_key.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![cfg(not(feature = "use_native_model"))]
#![cfg(not(feature = "native_model"))]
mod tests;

use serde::{Deserialize, Serialize};
Expand Down
2 changes: 1 addition & 1 deletion tests/09_iterator.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![cfg(not(feature = "use_native_model"))]
#![cfg(not(feature = "native_model"))]
mod tests;

use serde::{Deserialize, Serialize};
Expand Down
2 changes: 1 addition & 1 deletion tests/10_remove.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![cfg(not(feature = "use_native_model"))]
#![cfg(not(feature = "native_model"))]
mod tests;

use serde::{Deserialize, Serialize};
Expand Down
3 changes: 1 addition & 2 deletions tests/11_watch.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#![cfg(not(feature = "use_native_model"))]
#![cfg(not(feature = "native_model"))]
#![cfg(not(feature = "tokio"))]
mod tests;


use serde::{Deserialize, Serialize};
use std::sync::Arc;
use std::time::Duration;
Expand Down
4 changes: 2 additions & 2 deletions tests/11_watch_tokio.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![cfg(not(feature = "use_native_model"))]
#![cfg(not(feature = "native_model"))]
#![cfg(feature = "tokio")]
mod tests;

Expand Down Expand Up @@ -59,4 +59,4 @@ async fn watch_one_primary_key() {
assert!(recv.try_recv().is_err());
}

// TODO: maybe do others tests but it should the same as a std::sync::mpsc::channel.
// TODO: maybe do others tests but it should the same as a std::sync::mpsc::channel.
2 changes: 1 addition & 1 deletion tests/12_migration.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![cfg(not(feature = "use_native_model"))]
#![cfg(not(feature = "native_model"))]
mod tests;

use serde::{Deserialize, Serialize};
Expand Down
10 changes: 6 additions & 4 deletions tests/13_util.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![cfg(not(feature = "use_native_model"))]
#![cfg(not(feature = "native_model"))]
mod tests;

use struct_db::*;
Expand All @@ -14,10 +14,12 @@ fn test_builder() {
fn test_builder_with_set_cache_size() {
let tf = tests::init();
// Create without error
let mut _db = Builder::new().set_cache_size(100).create(&tf.path("test")).unwrap();
let mut _db = Builder::new()
.set_cache_size(100)
.create(&tf.path("test"))
.unwrap();
}


#[test]
fn test_open_unexisting_database() {
let tf = tests::init();
Expand All @@ -30,7 +32,7 @@ fn test_open_existing_database() {
let tf = tests::init();

// Create a database
let db = Builder::new().create(&tf.path("test")).unwrap();
let db = Builder::new().create(&tf.path("test")).unwrap();
drop(db);

// Open an existing database
Expand Down
2 changes: 1 addition & 1 deletion tests/14_native_model.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![cfg(feature = "use_native_model")]
#![cfg(feature = "native_model")]

mod tests;

Expand Down
2 changes: 1 addition & 1 deletion tests/modules.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[cfg(feature = "use_native_model")]
#[cfg(feature = "native_model")]
mod migrate;

mod primary_drain;
2 changes: 1 addition & 1 deletion tests/primary_drain/only_primary_key.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![cfg(not(feature = "use_native_model"))]
#![cfg(not(feature = "native_model"))]

use serde::{Deserialize, Serialize};
use shortcut_assert_fs::TmpFs;
Expand Down
2 changes: 1 addition & 1 deletion tests/primary_drain/with_secondary_keys.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![cfg(not(feature = "use_native_model"))]
#![cfg(not(feature = "native_model"))]

use redb::TableHandle;
use serde::{Deserialize, Serialize};
Expand Down

0 comments on commit ca9d6cc

Please sign in to comment.