Skip to content

Commit 667608f

Browse files
committed
Remove unsafe and broken bit flag implementation. Also general CI updates
1 parent ed15645 commit 667608f

File tree

6 files changed

+118
-82
lines changed

6 files changed

+118
-82
lines changed
Lines changed: 46 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,46 @@
1-
name: Release crate
2-
3-
on:
4-
workflow_dispatch:
5-
inputs:
6-
version:
7-
description: "major/minor/patch or semver"
8-
required: false
9-
default: "patch"
10-
11-
concurrency: release-crate
12-
13-
jobs:
14-
publish:
15-
runs-on: ubuntu-latest
16-
permissions:
17-
contents: write
18-
steps:
19-
- uses: actions/checkout@v4
20-
- name: Set git credentials
21-
run: |
22-
git config user.name github-actions
23-
git config user.email [email protected]
24-
- name: Crates publish
25-
uses: kaleidawave/crates-release-gh-action@main
26-
id: release
27-
with:
28-
version: ${{ github.event.inputs.version }}
29-
crates-token: ${{ secrets.CARGO_REGISTRY_TOKEN }}
30-
- name: Push updated Cargo.toml
31-
run: |
32-
git add .
33-
git commit -m "Release: ${{ steps.release.outputs.new-versions-description }}"
34-
git tag "release/${{ steps.release.outputs.new-version }}"
35-
git push --tags origin main
36-
- name: Discord
37-
uses: dusmartijngames/discord-webhook-notify@master
38-
with:
39-
severity: info
40-
text: "Released version ${{ steps.release.outputs.new-versions-description }}"
41-
webhookUrl: ${{ secrets.DISCORD_WEBHOOK_ENDPOINT }}
1+
name: Release crate
2+
3+
on:
4+
workflow_dispatch:
5+
inputs:
6+
version:
7+
description: "major/minor/patch or semver"
8+
required: false
9+
default: "patch"
10+
11+
concurrency: release-crate
12+
13+
jobs:
14+
publish:
15+
runs-on: ubuntu-latest
16+
permissions:
17+
contents: write
18+
steps:
19+
- id: checkout
20+
uses: actions/checkout@v4
21+
22+
- id: auth
23+
uses: rust-lang/crates-io-auth-action@v1
24+
25+
- id: release
26+
name: Crates publish
27+
uses: kaleidawave/crates-release-gh-action@improvements
28+
with:
29+
version: ${{ github.event.inputs.version }}
30+
crates-token: ${{ steps.auth.outputs.token }}
31+
32+
- name: Push updated Cargo.toml
33+
run: |
34+
git config user.name "github-actions[bot]"
35+
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
36+
git add -u
37+
git commit -m "Release: ${{ steps.release.outputs.new-version }}"
38+
git tag "release/${{ steps.release.outputs.new-version }}"
39+
git push --tags origin ${{ steps.checkout.outputs.ref }}
40+
41+
- name: Discord
42+
uses: dusmartijngames/discord-webhook-notify@master
43+
with:
44+
severity: info
45+
text: "Released version ${{ steps.release.outputs.new-versions-description }}"
46+
webhookUrl: ${{ secrets.DISCORD_WEBHOOK_ENDPOINT }}

.github/workflows/rust.yml

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,24 @@ jobs:
2424
with:
2525
path: ${{ env.CACHE_PATHS }}
2626
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
27+
2728
- name: Check source is valid
2829
run: cargo check --workspace
2930

3031
formating:
3132
runs-on: ubuntu-latest
3233
steps:
3334
- uses: actions/checkout@v4
35+
3436
- name: Check formatting with rustfmt
3537
run: cargo fmt --all --check
36-
- uses: brndnmtthws/rust-action-cargo-binstall@v1
38+
39+
- uses: kaleidawave/release-downloader@improvements
3740
with:
38-
packages: taplo-cli
39-
- name: Check TOML formatting with taplo
40-
run: |
41-
taplo fmt --check **/*/Cargo.toml
41+
items: kaleidawave/simple-toml-parser@canary[format]
42+
43+
- name: Check 'Cargo.toml' formatting
44+
run: format Cargo.toml --check
4245

4346
tests:
4447
needs: validity
@@ -49,6 +52,7 @@ jobs:
4952
with:
5053
path: ${{ env.CACHE_PATHS }}
5154
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
55+
5256
- name: Run all tests
5357
run: cargo test --workspace --verbose --all-features
5458

@@ -61,18 +65,28 @@ jobs:
6165
with:
6266
path: ${{ env.CACHE_PATHS }}
6367
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
68+
6469
- name: Lint code with clippy
6570
run: cargo clippy
6671

6772
publish-ability:
6873
runs-on: ubuntu-latest
69-
if: false
7074
steps:
71-
- uses: actions/checkout@v4
72-
- name: Check that it will publish to crates
73-
run: |
74-
cargo metadata --offline --format-version 1 --no-deps | jq -r ".workspace_members[]" | while read -r _n _v pathInfo ; do
75-
cd ${pathInfo:13:-1}
76-
cargo publish --no-verify --dry-run
77-
done
78-
shell: bash
75+
- uses: actions/checkout@v4
76+
- uses: dtolnay/rust-toolchain@stable
77+
78+
- uses: dorny/paths-filter@v3
79+
id: changes
80+
with:
81+
filters: |
82+
manifests:
83+
- '**/*.toml'
84+
85+
- uses: kaleidawave/release-downloader@improvements
86+
if: steps.changes.outputs.manifests == 'true'
87+
with:
88+
items: kaleidawave/crates-release-gh-action@assets[crates-release]
89+
90+
- name: Check that it will publish to crates.io
91+
if: steps.changes.outputs.manifests == 'true'
92+
run: crates-release verify

Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ name = "derive-enum-from-into"
33
version = "0.2.0"
44
edition = "2018"
55
description = "Derive for From and TryInto for enums with single fields"
6-
authors = [ "Ben <[email protected]>" ]
7-
categories = [ "rust-patterns" ]
6+
authors = ["Ben <[email protected]>"]
7+
categories = ["rust-patterns"]
88
license = "MIT"
99
repository = "https://github.com/kaleidawave/derive-enum-from-into"
10+
documentation = "https://docs.rs/derive-enum-from-into"
1011

1112
[lib]
1213
name = "derive_enum_from_into"

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
# Derive enum from try into
1+
# Derive enum from into
22

3-
[![](https://img.shields.io/crates/v/derive-enum-from-into)](https://crates.io/crates/derive-enum-from-into)
3+
[![crates.io badge](https://img.shields.io/crates/v/derive-enum-from-into?style=flat-square)](https://crates.io/crates/derive-enum-from-into)
4+
[![docs.rs badge](https://img.shields.io/docsrs/derive-enum-from-into?style=flat-square)](https://docs.rs/derive-enum-from-into/latest)
45

56
Implements `From` and `TryInto` for enums
67

src/into.rs

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
use std::{collections::HashMap, mem};
2-
31
use proc_macro2::Span;
42
use quote::quote;
3+
use std::collections::HashMap;
54
use syn::{
65
parse::Parse, parse_quote, token::Comma, Attribute, DataEnum, Error, GenericArgument,
76
GenericParam, Generics, Ident, Lifetime, LifetimeParam, Path, PathArguments, Token, Type,
@@ -108,20 +107,36 @@ pub(super) fn derive_enum_into(
108107
.collect::<proc_macro2::TokenStream>()
109108
}
110109

111-
#[repr(u8)]
112-
#[derive(Copy, Clone)]
113-
#[allow(dead_code)]
114-
enum ReferenceConfig {
115-
Owned = 0b0001,
116-
Shared = 0b0010,
117-
SharedMut = 0b0100,
118-
None = 0b0000,
119-
All = 0b0111,
110+
#[derive(Copy, Clone, PartialEq, Eq)]
111+
struct ReferenceConfig(pub u8);
112+
113+
impl ReferenceConfig {
114+
const OWNED: Self = Self(0b0001);
115+
const SHARED: Self = Self(0b0010);
116+
const SHARED_MUT: Self = Self(0b0100);
117+
const NONE: Self = Self(0b0000);
118+
// const ALL: Self = Self(0b0111);
119+
}
120+
121+
impl std::ops::BitOr for ReferenceConfig {
122+
type Output = Self;
123+
124+
fn bitor(self, rhs: Self) -> Self::Output {
125+
Self(self.0 | rhs.0)
126+
}
127+
}
128+
129+
impl std::ops::BitAnd for ReferenceConfig {
130+
type Output = Self;
131+
132+
fn bitand(self, rhs: Self) -> Self::Output {
133+
Self(self.0 & rhs.0)
134+
}
120135
}
121136

122137
impl Default for ReferenceConfig {
123138
fn default() -> Self {
124-
Self::Owned
139+
Self::OWNED
125140
}
126141
}
127142

@@ -134,24 +149,23 @@ impl Parse for ReferenceConfig {
134149
input.parse::<proc_macro2::TokenTree>().unwrap();
135150
if input.peek(Token![mut]) {
136151
input.parse::<Token![mut]>().unwrap();
137-
Ok(ReferenceConfig::SharedMut)
152+
Ok(ReferenceConfig::SHARED_MUT)
138153
} else {
139-
Ok(ReferenceConfig::Shared)
154+
Ok(ReferenceConfig::SHARED)
140155
}
141156
} else if input.peek(Ident) && input.parse::<Ident>().unwrap() == "owned" {
142-
Ok(ReferenceConfig::Owned)
157+
Ok(ReferenceConfig::OWNED)
143158
} else {
144159
Err(Error::new(input.span(), "expected 'ref', '&' or 'owned'"))
145160
}
146161
},
147162
Token![,],
148163
)?
149164
.into_iter()
150-
.map(|member| member as u8)
151165
.reduce(std::ops::BitOr::bitor)
152166
.unwrap_or_default();
153167

154-
Ok(unsafe { mem::transmute(flags) })
168+
Ok(flags)
155169
}
156170
}
157171

@@ -164,16 +178,16 @@ impl ReferenceConfig {
164178
(0..3)
165179
.map(|bit_offset| 0b0001_u8 << bit_offset)
166180
.filter_map(move |power_of_two| {
167-
let flag = self as u8 & power_of_two;
168-
match unsafe { mem::transmute(flag) } {
169-
ReferenceConfig::Owned => Some(ReferenceDecorator::Owned),
170-
ReferenceConfig::Shared => {
181+
let flag = self & ReferenceConfig(power_of_two);
182+
match flag {
183+
ReferenceConfig::OWNED => Some(ReferenceDecorator::Owned),
184+
ReferenceConfig::SHARED => {
171185
Some(ReferenceDecorator::Shared(lifetime_name.clone()))
172186
}
173-
ReferenceConfig::SharedMut => {
187+
ReferenceConfig::SHARED_MUT => {
174188
Some(ReferenceDecorator::SharedMut(lifetime_name.clone()))
175189
}
176-
ReferenceConfig::None => None,
190+
ReferenceConfig::NONE => None,
177191
_ => unreachable!(),
178192
}
179193
})

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![doc = include_str!("../README.md")]
2+
23
mod from;
34
mod into;
45

0 commit comments

Comments
 (0)