Skip to content

Commit

Permalink
any: add separate any trait for Plugin and Target
Browse files Browse the repository at this point in the history
  • Loading branch information
chensoft committed Mar 12, 2024
1 parent 61abebd commit eab2bf6
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 16 deletions.
7 changes: 6 additions & 1 deletion HISTORY.md
Expand Up @@ -11,8 +11,13 @@

### Changed

- Remove the parking_lot dependency
- Relax the requirements for Plugin and Target
- Add separate any trait for Plugin and Target

### Removed

- The parking_lot dependency
- Do not pub Any to avoid conflict

## [0.3.2] - 2024-03-07

Expand Down
14 changes: 1 addition & 13 deletions src/define.rs
@@ -1,21 +1,9 @@
//! Log levels and Encode trait
pub(crate) use std::any::Any;
pub(crate) use std::io::Write;
pub(crate) use std::path::Path;
pub(crate) use std::sync::Mutex;

/// Any support
pub trait Any: std::any::Any {
/// Treat object as any
fn as_any(&self) -> &dyn std::any::Any;
}

impl<T: std::any::Any> Any for T {
#[inline]
fn as_any(&self) -> &dyn std::any::Any {
self
}
}

/// Log Level
///
/// This type is simply an alias for i32. It was not implemented as an enum to allow for extension.
Expand Down
15 changes: 14 additions & 1 deletion src/plugin.rs
Expand Up @@ -9,7 +9,7 @@ use super::record::*;
///
/// You can terminate the log processing in advance, simply return `false` in `pre` or `post`.
#[allow(unused_variables)]
pub trait Plugin: Any + 'static {
pub trait Plugin: AnyPlugin + 'static {
/// Invoked before the `msg` field is appended to a record
#[inline]
#[must_use]
Expand All @@ -25,6 +25,19 @@ pub trait Plugin: Any + 'static {
}
}

/// Any Support
pub trait AnyPlugin: Any {
/// Treat object as any
fn as_any(&self) -> &dyn Any;
}

impl<T: Any> AnyPlugin for T {
#[inline]
fn as_any(&self) -> &dyn Any {
self
}
}

/// Add a level string to a record
///
/// ```json,no_run
Expand Down
15 changes: 14 additions & 1 deletion src/target.rs
Expand Up @@ -5,11 +5,24 @@ use super::define::*;
///
/// After completion, a record is directed to targets, whose purpose is to output the record's
/// content to various locations. A single record can be associated with multiple targets.
pub trait Target: Any + 'static {
pub trait Target: AnyTarget + 'static {
/// Write logs from buf to target
fn write(&self, buf: &[u8]);
}

/// Any Support
pub trait AnyTarget: Any {
/// Treat object as any
fn as_any(&self) -> &dyn Any;
}

impl<T: Any> AnyTarget for T {
#[inline]
fn as_any(&self) -> &dyn Any {
self
}
}

/// Write to stdout
///
/// ```
Expand Down

0 comments on commit eab2bf6

Please sign in to comment.