From 477f3a12bc136af4578d379f0f38d7545d347d2b Mon Sep 17 00:00:00 2001 From: Denis Kotlyarov Date: Mon, 13 May 2024 01:12:53 +0300 Subject: [PATCH] Update README.md --- README.md | 210 +++++++++++++++++++++++++----------------------------- 1 file changed, 99 insertions(+), 111 deletions(-) diff --git a/README.md b/README.md index 8322ee5..ec6b91c 100644 --- a/README.md +++ b/README.md @@ -1,146 +1,134 @@ -# cluFullTransmute -[![CI](https://github.com/clucompany/cluFullTransmute/actions/workflows/CI.yml/badge.svg?event=push)](https://github.com/clucompany/cluFullTransmute/actions/workflows/CI.yml) -[![Build Status](https://travis-ci.org/clucompany/cluFullTransmute.svg?branch=master)](https://travis-ci.org/clucompany/cluFullTransmute) -[![Apache licensed](https://img.shields.io/badge/license-Apache%202.0-blue.svg)](./LICENSE) -[![crates.io](https://img.shields.io/crates/v/cluFullTransmute)](https://crates.io/crates/cluFullTransmute) -[![Documentation](https://docs.rs/cluFullTransmute/badge.svg)](https://docs.rs/cluFullTransmute) - -A more complete and extended version of data type conversion without constraint checks. + -# !!! ATTENTION !!! +## !!! ATTENTION !!! 1. When converting types without checking the size of the data, you really need to understand what you are doing. 2. You must understand the specifics of the platform you are using. +## Usage: + +Add this to your Cargo.toml: -# Use +```toml +[dependencies] +cluFullTransmute = "1.3.0" +``` + +and this to your source code: +```rust +use cluFullTransmute::mem::transmute; +``` + +## Library Features -### 1. GenericType +1. Casting any type A to any type B with generic data without and with data dimension checking. +2. Ability to use transmutation in constant functions in very old versions of rust.. +3. Possibility of delayed transmutation through contracts. +4. Ability to work without the standard library. +## Example: ```rust +use cluFullTransmute::transmute_or_panic; use core::fmt::Display; -use cluFullTransmute::transmute::transmute_or_panic; -/// Implementation of a simple transmutation with a generic parameter inside. +/* + For example, let's write some code with a Drop trait that panics when dropped and + holds some data. We then transmute this data to another similar struct and check + that we have effectively overridden the Drop trait and have a different struct + with some data. + + We can also remove the Drop trait altogether or do any number of other things. +*/ +/// Struct to panic when dropped #[derive(Debug)] #[repr(transparent)] -struct A { - #[allow(dead_code)] - data: T -} +struct PanicWhenDrop(T); -impl Drop for A { +impl Drop for PanicWhenDrop { fn drop(&mut self) { - panic!("Invalid beh"); + panic!("panic, discovered `drop(PanicWhenDrop);`"); } } +/// Struct to print value when dropped #[derive(Debug)] #[repr(transparent)] -struct B where T: Display { - data: T, -} - -impl Drop for B where T: Display { +struct PrintlnWhenDrop(T) +where + T: Display; + +impl Drop for PrintlnWhenDrop +where + T: Display, +{ fn drop(&mut self) { - println!("{}", self.data); + println!("println: {}", self.0); } } fn main() { - let a: A = A { // original and panic when falling - data: 1024 - }; - println!("in: {:?}", a); - - let b: B = unsafe { transmute_or_panic(a) }; - println!("out: {:?}", b); - - drop(b); // <--- println! -} -``` + let a: PanicWhenDrop = PanicWhenDrop(1024); + println!("in a: {:?}", a); -### 2. Contract + let b: PrintlnWhenDrop = unsafe { transmute_or_panic(a as PanicWhenDrop) }; + println!("out b: {:?}", b); -```rust -use cluFullTransmute::contract::Contract; - -/* - For example, we will sign a contract to convert a String to a Vec, - although this may not be exactly the case. - - Contracts are needed to create more secure APIs using transmutation in - situations where it can't be proven. -*/ - -/// -struct MyData { - data: Contract<&'static str, &'static [u8]>, -} - -impl MyData { - #[inline] - const fn new(data: &'static str) -> Self { - let data = unsafe { - // Contract::force_new - // - - // The `checksize_new_or_panic` function can only guarantee equality of data - // dimensions, creating a contract is always unsafe, since the transmutation - // of such data types can only be proven orally. But after signing the - // transmutation contract, all functions for working with the transmuted are - // not marked as unsafe. - // - Contract::checksize_new_or_panic(data) - }; - Self { - data, - } - } - - #[inline] - pub fn as_data(&self) -> &'static str { - &self.data - } - - #[inline] - pub fn as_sliceu8(&self) -> &'static [u8] { - self.data.as_datato() - } - - #[inline] - pub fn into(self) -> &'static [u8] { - self.data.into() - } -} - - -fn main() { - const C_DATA: &'static str = "Test"; - - // &'static str - let data = MyData::new(C_DATA); - assert_eq!(data.as_data(), C_DATA); // const_readtype: &'static str - assert_eq!(data.as_sliceu8(), C_DATA.as_bytes()); //const_readtype &'static [u8] - // - - // &'static u8 - let vec = data.into(); // const_transmute: &'static str -> &'static [u8] - assert_eq!(vec, C_DATA.as_bytes()); + drop(b); // <--- drop, PrintlnWhenDrop! } ``` + + See all + -# License +## License: +This project has a single license (LICENSE-APACHE-2.0). -Copyright 2022 #UlinProject Denis Kotlyarov (Денис Котляров) - -Licensed under the Apache License, Version 2.0 +
+ + uproject + +  Copyright (c) 2022-2024 #UlinProject + +  (Denis Kotlyarov). +


+
+ +### Apache License: +
+ + apache2 + + +  Licensed under the Apache License, Version 2.0. +



+