Exemple #1
0
        void WriteEnumCore(FileWriter writer, PartialEnumFileInfo info, EnumType enumType)
        {
            docWriter.WriteSummary(writer, enumType.Documentation, enumType.RawName);
            var enumTypeName = enumType.Name(idConverter);

            foreach (var attr in info.Attributes)
            {
                writer.WriteLine(attr);
            }
            if (enumType.IsPublic && enumType.IsMissingDocs)
            {
                writer.WriteLine(RustConstants.AttributeAllowMissingDocs);
            }
            if (!enumType.IsPublic)
            {
                writer.WriteLine(RustConstants.AttributeAllowDeadCode);
            }
            var pub = enumType.IsPublic ? "pub " : "pub(crate) ";

            writer.WriteLine($"{pub}enum {enumTypeName} {{");
            using (writer.Indent()) {
                uint expectedValue = 0;
                foreach (var value in enumType.Values)
                {
                    docWriter.WriteSummary(writer, value.Documentation, enumType.RawName);
                    deprecatedWriter.WriteDeprecated(writer, value);
                    if (expectedValue != value.Value || enumType.IsPublic)
                    {
                        writer.WriteLine($"{value.Name(idConverter)} = {value.Value},");
                    }
                    else
                    {
                        writer.WriteLine($"{value.Name(idConverter)},");
                    }
                    expectedValue = value.Value + 1;
                }
            }
            writer.WriteLine("}");

            var arrayName = idConverter.Constant("GenDebug" + enumType.RawName);
            var feature   = info.Attributes.FirstOrDefault(a => a.StartsWith(RustConstants.FeaturePrefix) && a.Contains("(feature"));

            if (!(feature is null))
            {
                writer.WriteLine(feature);
            }
            writer.WriteLine(RustConstants.AttributeNoRustFmt);
            writer.WriteLine($"static {arrayName}: [&str; {enumType.Values.Length}] = [");
            using (writer.Indent()) {
                for (int i = 0; i < enumType.Values.Length; i++)
                {
                    writer.WriteLine($"\"{enumType.Values[i].Name(idConverter)}\",");
                }
            }
            writer.WriteLine("];");

            // #[derive(Debug)] isn't used since it generates a big switch statement. This code
            // uses a simple array lookup which has very little code. For small enums the default
            // implementation might be better though.
            if (!(feature is null))
            {
                writer.WriteLine(feature);
            }
            writer.WriteLine($"impl fmt::Debug for {enumTypeName} {{");
            using (writer.Indent()) {
                writer.WriteLine(RustConstants.AttributeInline);
                writer.WriteLine($"fn fmt<'a>(&self, f: &mut fmt::Formatter<'a>) -> fmt::Result {{");
                using (writer.Indent()) {
                    writer.WriteLine($"write!(f, \"{{}}\", {arrayName}[*self as usize])?;");
                    writer.WriteLine("Ok(())");
                }
                writer.WriteLine("}");
            }
            writer.WriteLine("}");

            if (!(feature is null))
            {
                writer.WriteLine(feature);
            }
            writer.WriteLine($"impl Default for {enumTypeName} {{");
            using (writer.Indent()) {
                writer.WriteLine(RustConstants.AttributeMustUse);
                writer.WriteLine(RustConstants.AttributeInline);
                writer.WriteLine("fn default() -> Self {");
                using (writer.Indent()) {
                    var defaultValue = enumType.Values[0];
                    // The first one should always have value 0 (eg. "None" field), and if the first one doesn't
                    // have value 0, there must be no other enum fields == 0.
                    if (defaultValue.Value != 0 && enumType.Values.Any(a => a.Value == 0))
                    {
                        throw new InvalidOperationException();
                    }
                    writer.WriteLine($"{enumTypeName}::{defaultValue.Name(idConverter)}");
                }
                writer.WriteLine("}");
            }
            writer.WriteLine("}");
        }
Exemple #2
0
        void WriteEnumCore(FileWriter writer, PartialEnumFileInfo info, EnumType enumType)
        {
            docWriter.WriteSummary(writer, enumType.Documentation, enumType.RawName);
            var enumTypeName = enumType.Name(idConverter);

            foreach (var attr in info.Attributes)
            {
                writer.WriteLine(attr);
            }
            if (enumType.IsPublic && enumType.IsMissingDocs)
            {
                writer.WriteLine(RustConstants.AttributeAllowMissingDocs);
            }
            if (!enumType.IsPublic)
            {
                writer.WriteLine(RustConstants.AttributeAllowDeadCode);
            }
            var pub = enumType.IsPublic ? "pub " : "pub(crate) ";

            writer.WriteLine($"{pub}enum {enumTypeName} {{");
            // Identical enum values aren't allowed so just remove them
            var enumValues = enumType.Values.Where(a => !a.DeprecatedInfo.IsDeprecatedAndRenamed).ToArray();

            using (writer.Indent()) {
                uint expectedValue = 0;
                foreach (var value in enumValues)
                {
                    docWriter.WriteSummary(writer, value.Documentation, enumType.RawName);
                    deprecatedWriter.WriteDeprecated(writer, value);
                    if (expectedValue != value.Value || enumType.IsPublic)
                    {
                        writer.WriteLine($"{value.Name(idConverter)} = {value.Value},");
                    }
                    else
                    {
                        writer.WriteLine($"{value.Name(idConverter)},");
                    }
                    expectedValue = value.Value + 1;
                }
            }
            writer.WriteLine("}");

            var arrayName = idConverter.Constant("GenDebug" + enumType.RawName);
            var feature   = info.Attributes.FirstOrDefault(a => a.StartsWith(RustConstants.FeaturePrefix, StringComparison.Ordinal) && a.Contains("(feature", StringComparison.Ordinal));

            if (feature is not null)
            {
                writer.WriteLine(feature);
            }
            writer.WriteLine(RustConstants.AttributeNoRustFmt);
            writer.WriteLine($"static {arrayName}: [&str; {enumValues.Length}] = [");
            using (writer.Indent()) {
                foreach (var value in enumValues)
                {
                    writer.WriteLine($"\"{value.Name(idConverter)}\",");
                }
            }
            writer.WriteLine("];");

            // #[derive(Debug)] isn't used since it generates a big switch statement. This code
            // uses a simple array lookup which has very little code. For small enums the default
            // implementation might be better though.
            if (feature is not null)
            {
                writer.WriteLine(feature);
            }
            writer.WriteLine($"impl fmt::Debug for {enumTypeName} {{");
            using (writer.Indent()) {
                writer.WriteLine(RustConstants.AttributeInline);
                writer.WriteLine($"fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {{");
                using (writer.Indent())
                    writer.WriteLine($"write!(f, \"{{}}\", {arrayName}[*self as usize])");
                writer.WriteLine("}");
            }
            writer.WriteLine("}");

            if (feature is not null)
            {
                writer.WriteLine(feature);
            }
            writer.WriteLine($"impl Default for {enumTypeName} {{");
            using (writer.Indent()) {
                writer.WriteLine(RustConstants.AttributeMustUse);
                writer.WriteLine(RustConstants.AttributeInline);
                writer.WriteLine("fn default() -> Self {");
                using (writer.Indent()) {
                    var defaultValue = enumValues[0];
                    // The first one should always have value 0 (eg. "None" field), and if the first one doesn't
                    // have value 0, there must be no other enum fields == 0.
                    if (defaultValue.Value != 0 && enumValues.Any(a => a.Value == 0))
                    {
                        throw new InvalidOperationException();
                    }
                    writer.WriteLine($"{enumTypeName}::{defaultValue.Name(idConverter)}");
                }
                writer.WriteLine("}");
            }
            writer.WriteLine("}");

            if (enumType.IsPublic)
            {
                var enumIterType       = $"{enumTypeName}Iterator";
                var icedConstValue     = "IcedConstants::" + idConverter.Constant(IcedConstants.GetEnumCountName(enumType.TypeId));
                var enumUnderlyingType = GetUnderlyingTypeStr(enumType);

                // Associated method: values()

                if (feature is not null)
                {
                    writer.WriteLine(feature);
                }
                writer.WriteLine(RustConstants.AttributeNoRustFmt);
                writer.WriteLine($"impl {enumTypeName} {{");
                using (writer.Indent()) {
                    writer.WriteLine($"/// Iterates over all `{enumTypeName}` enum values");
                    writer.WriteLine("#[inline]");
                    writer.WriteLine($"pub fn values() -> impl Iterator<Item = {enumTypeName}> + DoubleEndedIterator + ExactSizeIterator + FusedIterator {{");
                    using (writer.Indent()) {
                        if (enumType.Values.Length == 1)
                        {
                            writer.WriteLine($"static VALUES: [{enumTypeName}; 1] = [{enumTypeName}::{enumType.Values[0].Name(idConverter)}];");
                            writer.WriteLine($"VALUES.iter().copied()");
                        }
                        else
                        {
                            writer.WriteLine("// SAFETY: all values 0-max are valid enum values");
                            writer.WriteLine($"(0..{icedConstValue}).map(|x| unsafe {{ mem::transmute::<{enumUnderlyingType}, {enumTypeName}>(x as {enumUnderlyingType}) }})");
                        }
                    }
                    writer.WriteLine("}");
                }
                writer.WriteLine("}");
                writer.WriteLine("#[test]");
                if (feature is not null)
                {
                    writer.WriteLine(feature);
                }
                writer.WriteLine(RustConstants.AttributeNoRustFmt);
                writer.WriteLine($"fn test_{enumTypeName.ToLowerInvariant()}_values() {{");
                using (writer.Indent()) {
                    writer.WriteLine($"let mut iter = {enumTypeName}::values();");
                    writer.WriteLine($"assert_eq!(iter.size_hint(), ({icedConstValue}, Some({icedConstValue})));");
                    writer.WriteLine($"assert_eq!(iter.len(), {icedConstValue});");
                    writer.WriteLine("assert!(iter.next().is_some());");
                    writer.WriteLine($"assert_eq!(iter.size_hint(), ({icedConstValue} - 1, Some({icedConstValue} - 1)));");
                    writer.WriteLine($"assert_eq!(iter.len(), {icedConstValue} - 1);");
                    writer.WriteLine();
                    writer.WriteLine($"let values: Vec<{enumTypeName}> = {enumTypeName}::values().collect();");
                    writer.WriteLine($"assert_eq!(values.len(), {icedConstValue});");
                    writer.WriteLine("for (i, value) in values.into_iter().enumerate() {");
                    using (writer.Indent())
                        writer.WriteLine("assert_eq!(i, value as usize);");
                    writer.WriteLine("}");
                    writer.WriteLine();
                    writer.WriteLine($"let values1: Vec<{enumTypeName}> = {enumTypeName}::values().collect();");
                    writer.WriteLine($"let mut values2: Vec<{enumTypeName}> = {enumTypeName}::values().rev().collect();");
                    writer.WriteLine("values2.reverse();");
                    writer.WriteLine("assert_eq!(values1, values2);");
                }
                writer.WriteLine("}");

                // impl trait TryFrom

                var tryFromTypes = new string[] {
                    "usize",
                    //"u32",
                };
                foreach (var tryFromType in tryFromTypes)
                {
                    var castToFromType = tryFromType == "usize" ? string.Empty : $" as {tryFromType}";
                    if (feature is not null)
                    {
                        writer.WriteLine(feature);
                    }
                    writer.WriteLine(RustConstants.AttributeNoRustFmt);
                    writer.WriteLine($"impl TryFrom<{tryFromType}> for {enumTypeName} {{");
                    using (writer.Indent()) {
                        writer.WriteLine("type Error = IcedError;");
                        writer.WriteLine("#[inline]");
                        writer.WriteLine($"fn try_from(value: {tryFromType}) -> Result<Self, Self::Error> {{");
                        using (writer.Indent()) {
                            writer.WriteLine($"if value < {icedConstValue}{castToFromType} {{");
                            using (writer.Indent()) {
                                if (enumType.Values.Length == 1)
                                {
                                    writer.WriteLine($"Ok({enumTypeName}::{enumType.Values[0].Name(idConverter)})");
                                }
                                else
                                {
                                    writer.WriteLine("// SAFETY: all values 0-max are valid enum values");
                                    writer.WriteLine($"Ok(unsafe {{ mem::transmute(value as {enumUnderlyingType}) }})");
                                }
                            }
                            writer.WriteLine("} else {");
                            using (writer.Indent())
                                writer.WriteLine($"Err(IcedError::new(\"Invalid {enumTypeName} value\"))");
                            writer.WriteLine("}");
                        }
                        writer.WriteLine("}");
                    }
                    writer.WriteLine("}");
                    if (feature is not null)
                    {
                        writer.WriteLine(feature);
                    }
                    writer.WriteLine("#[test]");
                    writer.WriteLine(RustConstants.AttributeNoRustFmt);
                    writer.WriteLine($"fn test_{enumTypeName.ToLowerInvariant()}_try_from_{tryFromType}() {{");
                    using (writer.Indent()) {
                        writer.WriteLine($"for value in {enumTypeName}::values() {{");
                        using (writer.Indent()) {
                            writer.WriteLine($"let converted = <{enumTypeName} as TryFrom<{tryFromType}>>::try_from(value as {tryFromType}).unwrap();");
                            writer.WriteLine("assert_eq!(converted, value);");
                        }
                        writer.WriteLine("}");
                        writer.WriteLine($"assert!(<{enumTypeName} as TryFrom<{tryFromType}>>::try_from({icedConstValue}{castToFromType}).is_err());");
                        writer.WriteLine($"assert!(<{enumTypeName} as TryFrom<{tryFromType}>>::try_from(core::{tryFromType}::MAX).is_err());");
                    }
                    writer.WriteLine("}");
                }
            }
        }